66import javax .validation .ConstraintViolationException ;
77
88import org .apache .commons .lang3 .exception .ExceptionUtils ;
9- import org .slf4j .Logger ;
10- import org .slf4j .LoggerFactory ;
119import org .springframework .dao .DataAccessException ;
1210import org .springframework .dao .DataIntegrityViolationException ;
1311import org .springframework .dao .InvalidDataAccessApiUsageException ;
3331import com .baeldung .common .web .exception .ValidationErrorDTO ;
3432
3533@ ControllerAdvice
36- public class RestResponseEntityExceptionHandler extends ResponseEntityExceptionHandler {
37- private Logger log = LoggerFactory .getLogger (getClass ());
38-
39- public RestResponseEntityExceptionHandler () {
40- super ();
41- }
42-
43- // API
34+ public class RestResponseEntityExceptionHandler extends ResponseEntityExceptionHandler {
4435
4536 // 400
4637
4738 @ Override
4839 protected final ResponseEntity <Object > handleHttpMessageNotReadable (final HttpMessageNotReadableException ex , final HttpHeaders headers , final HttpStatus status , final WebRequest request ) {
49- log .info ("Bad Request: {}" , ex .getMessage ());
50- log .debug ("Bad Request: " , ex );
40+ logger .info ("Bad Request: " + ex .getMessage ());
41+ logger .debug ("Bad Request: " , ex );
5142
5243 final ApiError apiError = message (HttpStatus .BAD_REQUEST , ex );
5344 return handleExceptionInternal (ex , apiError , headers , HttpStatus .BAD_REQUEST , request );
5445 }
5546
5647 @ Override
5748 protected final ResponseEntity <Object > handleMethodArgumentNotValid (final MethodArgumentNotValidException ex , final HttpHeaders headers , final HttpStatus status , final WebRequest request ) {
58- log .info ("Bad Request: {}" , ex .getMessage ());
59- log .debug ("Bad Request: " , ex );
49+ logger .info ("Bad Request: " + ex .getMessage ());
50+ logger .debug ("Bad Request: " , ex );
6051
6152 final BindingResult result = ex .getBindingResult ();
6253 final List <FieldError > fieldErrors = result .getFieldErrors ();
@@ -66,9 +57,9 @@ protected final ResponseEntity<Object> handleMethodArgumentNotValid(final Method
6657 }
6758
6859 @ ExceptionHandler (value = { ConstraintViolationException .class , DataIntegrityViolationException .class })
69- public final ResponseEntity <Object > handleBadRequest (final RuntimeException ex , final WebRequest request ) {
70- log .info ("Bad Request: {}" , ex .getLocalizedMessage ());
71- log .debug ("Bad Request: " , ex );
60+ protected final ResponseEntity <Object > handleBadRequest (final RuntimeException ex , final WebRequest request ) {
61+ logger .info ("Bad Request: " + ex .getLocalizedMessage ());
62+ logger .debug ("Bad Request: " , ex );
7263
7364 if (ExceptionUtils .getRootCauseMessage (ex ).contains ("uplicate" )) {
7465 final ApiError apiError = message (HttpStatus .CONFLICT , ex );
@@ -82,7 +73,7 @@ public final ResponseEntity<Object> handleBadRequest(final RuntimeException ex,
8273 // 403
8374
8475 @ ExceptionHandler ({ AccessDeniedException .class })
85- public ResponseEntity <Object > handleEverything (final AccessDeniedException ex , final WebRequest request ) {
76+ protected ResponseEntity <Object > handleEverything (final AccessDeniedException ex , final WebRequest request ) {
8677 logger .error ("403 Status Code" , ex );
8778
8879 final ApiError apiError = message (HttpStatus .FORBIDDEN , ex );
@@ -94,7 +85,7 @@ public ResponseEntity<Object> handleEverything(final AccessDeniedException ex, f
9485
9586 @ ExceptionHandler ({ EntityNotFoundException .class , MyEntityNotFoundException .class , MyResourceNotFoundException .class })
9687 protected ResponseEntity <Object > handleNotFound (final RuntimeException ex , final WebRequest request ) {
97- log .warn ("Not Found: {}" , ex .getMessage ());
88+ logger .warn ("Not Found: " + ex .getMessage ());
9889
9990 final ApiError apiError = message (HttpStatus .NOT_FOUND , ex );
10091 return handleExceptionInternal (ex , apiError , new HttpHeaders (), HttpStatus .NOT_FOUND , request );
@@ -104,7 +95,7 @@ protected ResponseEntity<Object> handleNotFound(final RuntimeException ex, final
10495
10596 @ ExceptionHandler ({ InvalidDataAccessApiUsageException .class , DataAccessException .class , MyConflictException .class })
10697 protected ResponseEntity <Object > handleConflict (final RuntimeException ex , final WebRequest request ) {
107- log .warn ("Conflict: {}" , ex .getMessage ());
98+ logger .warn ("Conflict: " + ex .getMessage ());
10899
109100 final ApiError apiError = message (HttpStatus .CONFLICT , ex );
110101 return handleExceptionInternal (ex , apiError , new HttpHeaders (), HttpStatus .CONFLICT , request );
@@ -114,7 +105,7 @@ protected ResponseEntity<Object> handleConflict(final RuntimeException ex, final
114105
115106 @ ExceptionHandler ({ InvalidMimeTypeException .class , InvalidMediaTypeException .class })
116107 protected ResponseEntity <Object > handleInvalidMimeTypeException (final IllegalArgumentException ex , final WebRequest request ) {
117- log .warn ("Unsupported Media Type: {}" , ex .getMessage ());
108+ logger .warn ("Unsupported Media Type: " + ex .getMessage ());
118109
119110 final ApiError apiError = message (HttpStatus .UNSUPPORTED_MEDIA_TYPE , ex );
120111 return handleExceptionInternal (ex , apiError , new HttpHeaders (), HttpStatus .UNSUPPORTED_MEDIA_TYPE , request );
@@ -123,7 +114,7 @@ protected ResponseEntity<Object> handleInvalidMimeTypeException(final IllegalArg
123114 // 500
124115
125116 @ ExceptionHandler ({ NullPointerException .class , IllegalArgumentException .class , IllegalStateException .class })
126- public ResponseEntity <Object > handle500s (final RuntimeException ex , final WebRequest request ) {
117+ protected ResponseEntity <Object > handle500s (final RuntimeException ex , final WebRequest request ) {
127118 logger .error ("500 Status Code" , ex );
128119
129120 final ApiError apiError = message (HttpStatus .INTERNAL_SERVER_ERROR , ex );
0 commit comments