1
1
package com .baeldung .web .error ;
2
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 ;
3
9
import org .springframework .http .HttpHeaders ;
4
10
import org .springframework .http .HttpStatus ;
5
11
import org .springframework .http .ResponseEntity ;
10
16
import org .springframework .web .context .request .WebRequest ;
11
17
import org .springframework .web .servlet .mvc .method .annotation .ResponseEntityExceptionHandler ;
12
18
13
- import com .baeldung .web .exception .MyDataAccessException ;
14
- import com .baeldung .web .exception .MyDataIntegrityViolationException ;
15
19
import com .baeldung .web .exception .MyResourceNotFoundException ;
16
20
17
21
@ ControllerAdvice
@@ -24,13 +28,15 @@ public RestResponseEntityExceptionHandler() {
24
28
// API
25
29
26
30
// 400
27
- /*
28
- * Some examples of exceptions that we could retrieve as 400 (BAD_REQUEST) responses:
29
- * Hibernate's ConstraintViolationException
30
- * Spring's DataIntegrityViolationException
31
- */
32
- @ ExceptionHandler ({ MyDataIntegrityViolationException .class })
33
- public ResponseEntity <Object > handleBadRequest (final MyDataIntegrityViolationException ex , final WebRequest request ) {
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 ) {
34
40
final String bodyOfResponse = "This should be application specific" ;
35
41
return handleExceptionInternal (ex , bodyOfResponse , new HttpHeaders (), HttpStatus .BAD_REQUEST , request );
36
42
}
@@ -50,23 +56,16 @@ protected ResponseEntity<Object> handleMethodArgumentNotValid(final MethodArgume
50
56
51
57
52
58
// 404
53
- /*
54
- * Some examples of exceptions that we could retrieve as 404 (NOT_FOUND) responses:
55
- * Java Persistence's EntityNotFoundException
56
- */
57
- @ ExceptionHandler (value = { MyResourceNotFoundException .class })
59
+
60
+ @ ExceptionHandler (value = { EntityNotFoundException .class , MyResourceNotFoundException .class })
58
61
protected ResponseEntity <Object > handleNotFound (final RuntimeException ex , final WebRequest request ) {
59
62
final String bodyOfResponse = "This should be application specific" ;
60
63
return handleExceptionInternal (ex , bodyOfResponse , new HttpHeaders (), HttpStatus .NOT_FOUND , request );
61
64
}
62
65
63
66
// 409
64
- /*
65
- * Some examples of exceptions that we could retrieve as 409 (CONFLICT) responses:
66
- * Spring's InvalidDataAccessApiUsageException
67
- * Spring's DataAccessException
68
- */
69
- @ ExceptionHandler ({ MyDataAccessException .class })
67
+
68
+ @ ExceptionHandler ({ InvalidDataAccessApiUsageException .class , DataAccessException .class })
70
69
protected ResponseEntity <Object > handleConflict (final RuntimeException ex , final WebRequest request ) {
71
70
final String bodyOfResponse = "This should be application specific" ;
72
71
return handleExceptionInternal (ex , bodyOfResponse , new HttpHeaders (), HttpStatus .CONFLICT , request );
@@ -83,4 +82,4 @@ protected ResponseEntity<Object> handleConflict(final RuntimeException ex, final
83
82
return handleExceptionInternal (ex , bodyOfResponse , new HttpHeaders (), HttpStatus .INTERNAL_SERVER_ERROR , request );
84
83
}
85
84
86
- }
85
+ }
0 commit comments