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 ;
3028import com .baeldung .common .web .exception .ValidationErrorDTO ;
3129
3230@ ControllerAdvice
33- public class RestResponseEntityExceptionHandler extends ResponseEntityExceptionHandler {
34- private Logger log = LoggerFactory .getLogger (getClass ());
35-
36- public RestResponseEntityExceptionHandler () {
37- super ();
38- }
39-
40- // API
31+ public class RestResponseEntityExceptionHandler extends ResponseEntityExceptionHandler {
4132
4233 // 400
4334
4435 @ Override
4536 protected final ResponseEntity <Object > handleHttpMessageNotReadable (final HttpMessageNotReadableException ex , final HttpHeaders headers , final HttpStatus status , final WebRequest request ) {
46- log .info ("Bad Request: {}" , ex .getMessage ());
47- log .debug ("Bad Request: " , ex );
37+ logger .info ("Bad Request: " + ex .getMessage ());
38+ logger .debug ("Bad Request: " , ex );
4839
4940 final ApiError apiError = message (HttpStatus .BAD_REQUEST , ex );
5041 return handleExceptionInternal (ex , apiError , headers , HttpStatus .BAD_REQUEST , request );
5142 }
5243
5344 @ Override
5445 protected final ResponseEntity <Object > handleMethodArgumentNotValid (final MethodArgumentNotValidException ex , final HttpHeaders headers , final HttpStatus status , final WebRequest request ) {
55- log .info ("Bad Request: {}" , ex .getMessage ());
56- log .debug ("Bad Request: " , ex );
46+ logger .info ("Bad Request: " + ex .getMessage ());
47+ logger .debug ("Bad Request: " , ex );
5748
5849 final BindingResult result = ex .getBindingResult ();
5950 final List <FieldError > fieldErrors = result .getFieldErrors ();
@@ -63,9 +54,9 @@ protected final ResponseEntity<Object> handleMethodArgumentNotValid(final Method
6354 }
6455
6556 @ ExceptionHandler (value = { ConstraintViolationException .class , DataIntegrityViolationException .class })
66- public final ResponseEntity <Object > handleBadRequest (final RuntimeException ex , final WebRequest request ) {
67- log .info ("Bad Request: {}" , ex .getLocalizedMessage ());
68- log .debug ("Bad Request: " , ex );
57+ protected final ResponseEntity <Object > handleBadRequest (final RuntimeException ex , final WebRequest request ) {
58+ logger .info ("Bad Request: " + ex .getLocalizedMessage ());
59+ logger .debug ("Bad Request: " , ex );
6960
7061 final ApiError apiError = message (HttpStatus .BAD_REQUEST , ex );
7162 return handleExceptionInternal (ex , apiError , new HttpHeaders (), HttpStatus .BAD_REQUEST , request );
@@ -79,7 +70,7 @@ public final ResponseEntity<Object> handleBadRequest(final RuntimeException ex,
7970 // 403
8071
8172 @ ExceptionHandler ({ AccessDeniedException .class })
82- public ResponseEntity <Object > handleEverything (final AccessDeniedException ex , final WebRequest request ) {
73+ protected ResponseEntity <Object > handleEverything (final AccessDeniedException ex , final WebRequest request ) {
8374 logger .error ("403 Status Code" , ex );
8475
8576 final ApiError apiError = message (HttpStatus .FORBIDDEN , ex );
@@ -91,7 +82,7 @@ public ResponseEntity<Object> handleEverything(final AccessDeniedException ex, f
9182
9283 @ ExceptionHandler ({ EntityNotFoundException .class , MyEntityNotFoundException .class , MyResourceNotFoundException .class })
9384 protected ResponseEntity <Object > handleNotFound (final MyEntityNotFoundException ex , final WebRequest request ) {
94- log .warn ("Not Found: {}" , ex .getMessage ());
85+ logger .warn ("Not Found: " + ex .getMessage ());
9586
9687 final ApiError apiError = message (HttpStatus .NOT_FOUND , ex );
9788 return handleExceptionInternal (ex , apiError , new HttpHeaders (), HttpStatus .NOT_FOUND , request );
@@ -101,7 +92,7 @@ protected ResponseEntity<Object> handleNotFound(final MyEntityNotFoundException
10192
10293 @ ExceptionHandler ({ InvalidDataAccessApiUsageException .class , DataAccessException .class , MyConflictException .class })
10394 protected ResponseEntity <Object > handleConflict (final RuntimeException ex , final WebRequest request ) {
104- log .warn ("Conflict: {}" , ex .getMessage ());
95+ logger .warn ("Conflict: " + ex .getMessage ());
10596
10697 final ApiError apiError = message (HttpStatus .CONFLICT , ex );
10798 return handleExceptionInternal (ex , apiError , new HttpHeaders (), HttpStatus .CONFLICT , request );
@@ -110,7 +101,7 @@ protected ResponseEntity<Object> handleConflict(final RuntimeException ex, final
110101 // 500
111102
112103 @ ExceptionHandler ({ NullPointerException .class , IllegalArgumentException .class , IllegalStateException .class })
113- public ResponseEntity <Object > handle500s (final RuntimeException ex , final WebRequest request ) {
104+ protected ResponseEntity <Object > handle500s (final RuntimeException ex , final WebRequest request ) {
114105 logger .error ("500 Status Code" , ex );
115106
116107 final ApiError apiError = message (HttpStatus .INTERNAL_SERVER_ERROR , ex );
0 commit comments