55import javax .persistence .EntityNotFoundException ;
66import javax .validation .ConstraintViolationException ;
77
8- import org .slf4j .Logger ;
9- import org .slf4j .LoggerFactory ;
108import org .springframework .dao .DataAccessException ;
119import org .springframework .dao .DataIntegrityViolationException ;
1210import org .springframework .dao .InvalidDataAccessApiUsageException ;
3129import com .baeldung .common .web .exception .ValidationErrorDTO ;
3230
3331@ ControllerAdvice
34- public class RestResponseEntityExceptionHandler extends ResponseEntityExceptionHandler {
35- private Logger log = LoggerFactory .getLogger (getClass ());
36-
37- public RestResponseEntityExceptionHandler () {
38- super ();
39- }
40-
41- // API
32+ public class RestResponseEntityExceptionHandler extends ResponseEntityExceptionHandler {
4233
4334 // 400
4435
4536 @ Override
4637 protected final ResponseEntity <Object > handleHttpMessageNotReadable (final HttpMessageNotReadableException ex , final HttpHeaders headers , final HttpStatus status , final WebRequest request ) {
47- log .info ("Bad Request: {} " , ex . getMessage () );
48- log .debug ("Bad Request: " , ex );
38+ logger .info ("Bad Request: " , ex );
39+ logger .debug ("Bad Request: " , ex );
4940
5041 final ApiError apiError = message (HttpStatus .BAD_REQUEST , ex );
5142 return handleExceptionInternal (ex , apiError , headers , HttpStatus .BAD_REQUEST , request );
5243 }
5344
5445 @ Override
5546 protected final ResponseEntity <Object > handleMethodArgumentNotValid (final MethodArgumentNotValidException ex , final HttpHeaders headers , final HttpStatus status , final WebRequest request ) {
56- log .info ("Bad Request: {} " , ex . getMessage () );
57- log .debug ("Bad Request: " , ex );
47+ logger .info ("Bad Request: " , ex );
48+ logger .debug ("Bad Request: " , ex );
5849
5950 final BindingResult result = ex .getBindingResult ();
6051 final List <FieldError > fieldErrors = result .getFieldErrors ();
@@ -64,9 +55,9 @@ protected final ResponseEntity<Object> handleMethodArgumentNotValid(final Method
6455 }
6556
6657 @ ExceptionHandler (value = { ConstraintViolationException .class , DataIntegrityViolationException .class })
67- public final ResponseEntity <Object > handleBadRequest (final RuntimeException ex , final WebRequest request ) {
68- log .info ("Bad Request: {} " , ex . getLocalizedMessage () );
69- log .debug ("Bad Request: " , ex );
58+ protected final ResponseEntity <Object > handleBadRequest (final RuntimeException ex , final WebRequest request ) {
59+ logger .info ("Bad Request: " , ex );
60+ logger .debug ("Bad Request: " , ex );
7061
7162 final ApiError apiError = message (HttpStatus .BAD_REQUEST , ex );
7263 return handleExceptionInternal (ex , apiError , new HttpHeaders (), HttpStatus .BAD_REQUEST , request );
@@ -76,7 +67,7 @@ public final ResponseEntity<Object> handleBadRequest(final RuntimeException ex,
7667
7768 @ ExceptionHandler ({ EntityNotFoundException .class , MyEntityNotFoundException .class , MyResourceNotFoundException .class })
7869 protected ResponseEntity <Object > handleNotFound (final RuntimeException ex , final WebRequest request ) {
79- log .warn ("Not Found: {} " , ex . getMessage () );
70+ logger .warn ("Not Found: " , ex );
8071
8172 final ApiError apiError = message (HttpStatus .NOT_FOUND , ex );
8273 return handleExceptionInternal (ex , apiError , new HttpHeaders (), HttpStatus .NOT_FOUND , request );
@@ -86,7 +77,7 @@ protected ResponseEntity<Object> handleNotFound(final RuntimeException ex, final
8677
8778 @ ExceptionHandler ({ InvalidDataAccessApiUsageException .class , DataAccessException .class , MyConflictException .class })
8879 protected ResponseEntity <Object > handleConflict (final RuntimeException ex , final WebRequest request ) {
89- log .warn ("Conflict: {} " , ex . getMessage () );
80+ logger .warn ("Conflict: " , ex );
9081
9182 final ApiError apiError = message (HttpStatus .CONFLICT , ex );
9283 return handleExceptionInternal (ex , apiError , new HttpHeaders (), HttpStatus .CONFLICT , request );
@@ -96,7 +87,7 @@ protected ResponseEntity<Object> handleConflict(final RuntimeException ex, final
9687
9788 @ ExceptionHandler ({ InvalidMimeTypeException .class , InvalidMediaTypeException .class })
9889 protected ResponseEntity <Object > handleInvalidMimeTypeException (final IllegalArgumentException ex , final WebRequest request ) {
99- log .warn ("Unsupported Media Type: {} " , ex . getMessage () );
90+ logger .warn ("Unsupported Media Type: " , ex );
10091
10192 final ApiError apiError = message (HttpStatus .UNSUPPORTED_MEDIA_TYPE , ex );
10293 return handleExceptionInternal (ex , apiError , new HttpHeaders (), HttpStatus .UNSUPPORTED_MEDIA_TYPE , request );
@@ -105,7 +96,7 @@ protected ResponseEntity<Object> handleInvalidMimeTypeException(final IllegalArg
10596 // 500
10697
10798 @ ExceptionHandler ({ NullPointerException .class , IllegalArgumentException .class , IllegalStateException .class })
108- public ResponseEntity <Object > handle500s (final RuntimeException ex , final WebRequest request ) {
99+ protected ResponseEntity <Object > handle500s (final RuntimeException ex , final WebRequest request ) {
109100 logger .error ("500 Status Code" , ex );
110101
111102 final ApiError apiError = message (HttpStatus .INTERNAL_SERVER_ERROR , ex );
0 commit comments