11package com .baeldung .common .web ;
22
3- import javax .persistence .EntityNotFoundException ;
43import javax .validation .ConstraintViolationException ;
54
65import org .apache .commons .lang3 .exception .ExceptionUtils ;
7- import org .springframework .dao .DataAccessException ;
6+ import org .slf4j .Logger ;
7+ import org .slf4j .LoggerFactory ;
88import org .springframework .dao .DataIntegrityViolationException ;
9- import org .springframework .dao .InvalidDataAccessApiUsageException ;
109import org .springframework .http .HttpHeaders ;
1110import org .springframework .http .HttpStatus ;
1211import org .springframework .http .ResponseEntity ;
1716import org .springframework .web .context .request .WebRequest ;
1817import org .springframework .web .servlet .mvc .method .annotation .ResponseEntityExceptionHandler ;
1918
20- import com .baeldung .common .persistence .exception .MyEntityNotFoundException ;
2119import com .baeldung .common .web .exception .ApiError ;
2220import com .baeldung .common .web .exception .MyBadRequestException ;
23- import com .baeldung .common .web .exception .MyConflictException ;
24- import com .baeldung .common .web .exception .MyForbiddenException ;
25- import com .baeldung .common .web .exception .MyPreconditionFailedException ;
2621
2722@ ControllerAdvice
2823public class RestResponseEntityExceptionHandler extends ResponseEntityExceptionHandler {
2924
25+ private Logger log = LoggerFactory .getLogger (getClass ());
26+
27+ public RestResponseEntityExceptionHandler () {
28+ super ();
29+ }
30+
31+ // API
32+
3033 // 400
3134
3235 @ Override
@@ -51,51 +54,4 @@ private final ApiError message(final HttpStatus httpStatus, final Exception ex)
5154 return new ApiError (httpStatus .value (), message , devMessage );
5255 }
5356
54- // 403
55-
56- @ ExceptionHandler ({ MyForbiddenException .class })
57- protected ResponseEntity <Object > handleForbidden (final MyForbiddenException ex , final WebRequest request ) {
58- final String bodyOfResponse = "This should be application specific" ;
59- return handleExceptionInternal (ex , bodyOfResponse , new HttpHeaders (), HttpStatus .FORBIDDEN , request );
60- }
61-
62- // 404
63-
64- @ ExceptionHandler ({ MyEntityNotFoundException .class })
65- protected ResponseEntity <Object > handleNotFound (final MyEntityNotFoundException ex , final WebRequest request ) {
66- final String bodyOfResponse = "This should be application specific" ;
67- return handleExceptionInternal (ex , bodyOfResponse , new HttpHeaders (), HttpStatus .NOT_FOUND , request );
68- }
69-
70- @ ExceptionHandler (value = { EntityNotFoundException .class })
71- protected ResponseEntity <Object > handleLibraryNotFound (final EntityNotFoundException ex , final WebRequest request ) {
72- final String bodyOfResponse = "This should be application specific" ;
73- return handleExceptionInternal (ex , bodyOfResponse , new HttpHeaders (), HttpStatus .NOT_FOUND , request );
74- }
75-
76- // 409
77-
78- @ ExceptionHandler ({ InvalidDataAccessApiUsageException .class , DataAccessException .class , MyConflictException .class })
79- protected ResponseEntity <Object > handleConflict (final RuntimeException ex , final WebRequest request ) {
80- final String bodyOfResponse = "This should be application specific" ;
81- return handleExceptionInternal (ex , bodyOfResponse , new HttpHeaders (), HttpStatus .CONFLICT , request );
82- }
83-
84- // 412
85-
86- @ ExceptionHandler ({ MyPreconditionFailedException .class })
87- protected ResponseEntity <Object > handlePreconditionFailed (final RuntimeException ex , final WebRequest request ) {
88- final String bodyOfResponse = "This should be application specific" ;
89- return handleExceptionInternal (ex , bodyOfResponse , new HttpHeaders (), HttpStatus .PRECONDITION_FAILED , request );
90- }
91-
92- // 500
93-
94- @ ExceptionHandler ({ NullPointerException .class , IllegalArgumentException .class , IllegalStateException .class })
95- protected ResponseEntity <Object > handleInternal (final RuntimeException ex , final WebRequest request ) {
96- logger .error ("500 Status Code" , ex );
97- final String bodyOfResponse = "This should be application specific" ;
98- return handleExceptionInternal (ex , bodyOfResponse , new HttpHeaders (), HttpStatus .INTERNAL_SERVER_ERROR , request );
99- }
100-
10157}
0 commit comments