|
16 | 16 | @ControllerAdvice
|
17 | 17 | public class ApplicationExceptionHandler extends ResponseEntityExceptionHandler {
|
18 | 18 |
|
19 |
| - @ExceptionHandler(value = {ConstraintViolationException.class}) |
20 |
| - protected ResponseEntity<Object> handleConstraintViolationException( |
21 |
| - ConstraintViolationException exception, ServletWebRequest request) { |
22 |
| - switch (request.getHeader(HttpHeaders.ACCEPT)) { |
23 |
| - case MediaType.TEXT_PLAIN_VALUE: |
24 |
| - StringBuilder stringBuilder = new StringBuilder(); |
25 |
| - for (ConstraintViolation violation : exception.getConstraintViolations()) { |
26 |
| - stringBuilder.append( |
27 |
| - violation.getPropertyPath().toString() + ": " + violation.getMessage() + '\n'); |
28 |
| - } |
| 19 | + @ExceptionHandler(value = { ConstraintViolationException.class }) |
| 20 | + protected ResponseEntity<Object> handleConstraintViolationException( |
| 21 | + ConstraintViolationException exception, ServletWebRequest request) { |
| 22 | + switch (request.getHeader(HttpHeaders.ACCEPT)) { |
| 23 | + case MediaType.TEXT_PLAIN_VALUE: |
| 24 | + StringBuilder stringBuilder = new StringBuilder(); |
| 25 | + for (ConstraintViolation violation : exception.getConstraintViolations()) { |
| 26 | + stringBuilder.append( |
| 27 | + violation.getPropertyPath().toString() + ": " + violation.getMessage() + '\n'); |
| 28 | + } |
29 | 29 |
|
30 |
| - return handleExceptionInternal( |
31 |
| - exception, |
32 |
| - stringBuilder.toString(), |
33 |
| - new HttpHeaders(), |
34 |
| - HttpStatus.BAD_REQUEST, |
35 |
| - request); |
36 |
| - default: |
37 |
| - LinkedHashMap<String, Object> constraintViolations = new LinkedHashMap<>(); |
38 |
| - for (ConstraintViolation violation : exception.getConstraintViolations()) { |
39 |
| - constraintViolations.put(violation.getPropertyPath().toString(), violation.getMessage()); |
40 |
| - } |
| 30 | + return handleExceptionInternal( |
| 31 | + exception, |
| 32 | + stringBuilder.toString(), |
| 33 | + new HttpHeaders(), |
| 34 | + HttpStatus.BAD_REQUEST, |
| 35 | + request); |
| 36 | + default: |
| 37 | + LinkedHashMap<String, Object> constraintViolations = new LinkedHashMap<>(); |
| 38 | + for (ConstraintViolation violation : exception.getConstraintViolations()) { |
| 39 | + constraintViolations.put(violation.getPropertyPath().toString(), violation.getMessage()); |
| 40 | + } |
41 | 41 |
|
42 |
| - LinkedHashMap<String, Object> body = new LinkedHashMap<>(); |
43 |
| - body.put("timestamp", new Date()); |
44 |
| - body.put("status", HttpStatus.BAD_REQUEST.value()); |
45 |
| - body.put("error", HttpStatus.BAD_REQUEST.getReasonPhrase()); |
46 |
| - body.put("message", exception.getMessage()); |
47 |
| - body.put("violations", constraintViolations); |
| 42 | + LinkedHashMap<String, Object> body = new LinkedHashMap<>(); |
| 43 | + body.put("timestamp", new Date()); |
| 44 | + body.put("status", HttpStatus.BAD_REQUEST.value()); |
| 45 | + body.put("error", HttpStatus.BAD_REQUEST.getReasonPhrase()); |
| 46 | + body.put("message", exception.getMessage()); |
| 47 | + body.put("violations", constraintViolations); |
48 | 48 |
|
49 |
| - return handleExceptionInternal( |
50 |
| - exception, body, new HttpHeaders(), HttpStatus.BAD_REQUEST, request); |
| 49 | + return handleExceptionInternal( |
| 50 | + exception, body, new HttpHeaders(), HttpStatus.BAD_REQUEST, request); |
| 51 | + } |
51 | 52 | }
|
52 |
| - } |
53 | 53 | }
|
0 commit comments