1
+ package com .baeldung .web .error ;
2
+
3
+ import javax .persistence .EntityNotFoundException ;
4
+
5
+ import org .hibernate .exception .ConstraintViolationException ;
6
+ import org .springframework .dao .DataAccessException ;
7
+ import org .springframework .dao .DataIntegrityViolationException ;
8
+ import org .springframework .dao .InvalidDataAccessApiUsageException ;
9
+ import org .springframework .http .HttpHeaders ;
10
+ import org .springframework .http .HttpStatus ;
11
+ import org .springframework .http .ResponseEntity ;
12
+ import org .springframework .http .converter .HttpMessageNotReadableException ;
13
+ import org .springframework .web .bind .MethodArgumentNotValidException ;
14
+ import org .springframework .web .bind .annotation .ControllerAdvice ;
15
+ import org .springframework .web .bind .annotation .ExceptionHandler ;
16
+ import org .springframework .web .context .request .WebRequest ;
17
+ import org .springframework .web .servlet .mvc .method .annotation .ResponseEntityExceptionHandler ;
18
+
19
+ import com .baeldung .web .exception .MyResourceNotFoundException ;
20
+
21
+ @ ControllerAdvice
22
+ public class RestResponseEntityExceptionHandler extends ResponseEntityExceptionHandler {
23
+
24
+ public RestResponseEntityExceptionHandler () {
25
+ super ();
26
+ }
27
+
28
+ // API
29
+
30
+ // 400
31
+
32
+ @ ExceptionHandler ({ ConstraintViolationException .class })
33
+ public ResponseEntity <Object > handleBadRequest (final ConstraintViolationException ex , final WebRequest request ) {
34
+ final String bodyOfResponse = "This should be application specific" ;
35
+ return handleExceptionInternal (ex , bodyOfResponse , new HttpHeaders (), HttpStatus .BAD_REQUEST , request );
36
+ }
37
+
38
+ @ ExceptionHandler ({ DataIntegrityViolationException .class })
39
+ public ResponseEntity <Object > handleBadRequest (final DataIntegrityViolationException ex , final WebRequest request ) {
40
+ final String bodyOfResponse = "This should be application specific" ;
41
+ return handleExceptionInternal (ex , bodyOfResponse , new HttpHeaders (), HttpStatus .BAD_REQUEST , request );
42
+ }
43
+
44
+ @ Override
45
+ protected ResponseEntity <Object > handleHttpMessageNotReadable (final HttpMessageNotReadableException ex , final HttpHeaders headers , final HttpStatus status , final WebRequest request ) {
46
+ final String bodyOfResponse = "This should be application specific" ;
47
+ // ex.getCause() instanceof JsonMappingException, JsonParseException // for additional information later on
48
+ return handleExceptionInternal (ex , bodyOfResponse , headers , HttpStatus .BAD_REQUEST , request );
49
+ }
50
+
51
+ @ Override
52
+ protected ResponseEntity <Object > handleMethodArgumentNotValid (final MethodArgumentNotValidException ex , final HttpHeaders headers , final HttpStatus status , final WebRequest request ) {
53
+ final String bodyOfResponse = "This should be application specific" ;
54
+ return handleExceptionInternal (ex , bodyOfResponse , headers , HttpStatus .BAD_REQUEST , request );
55
+ }
56
+
57
+
58
+ // 404
59
+
60
+ @ ExceptionHandler (value = { EntityNotFoundException .class , MyResourceNotFoundException .class })
61
+ protected ResponseEntity <Object > handleNotFound (final RuntimeException ex , final WebRequest request ) {
62
+ final String bodyOfResponse = "This should be application specific" ;
63
+ return handleExceptionInternal (ex , bodyOfResponse , new HttpHeaders (), HttpStatus .NOT_FOUND , request );
64
+ }
65
+
66
+ // 409
67
+
68
+ @ ExceptionHandler ({ InvalidDataAccessApiUsageException .class , DataAccessException .class })
69
+ protected ResponseEntity <Object > handleConflict (final RuntimeException ex , final WebRequest request ) {
70
+ final String bodyOfResponse = "This should be application specific" ;
71
+ return handleExceptionInternal (ex , bodyOfResponse , new HttpHeaders (), HttpStatus .CONFLICT , request );
72
+ }
73
+
74
+ // 412
75
+
76
+ // 500
77
+
78
+ @ ExceptionHandler ({ NullPointerException .class , IllegalArgumentException .class , IllegalStateException .class })
79
+ /*500*/ public ResponseEntity <Object > handleInternal (final RuntimeException ex , final WebRequest request ) {
80
+ logger .error ("500 Status Code" , ex );
81
+ final String bodyOfResponse = "This should be application specific" ;
82
+ return handleExceptionInternal (ex , bodyOfResponse , new HttpHeaders (), HttpStatus .INTERNAL_SERVER_ERROR , request );
83
+ }
84
+
85
+ }
0 commit comments