|
1 | 1 | package com.baeldung.web.error; |
2 | 2 |
|
| 3 | +import com.baeldung.web.exception.CustomException1; |
| 4 | +import com.baeldung.web.exception.CustomException2; |
3 | 5 | import com.baeldung.web.exception.CustomException3; |
4 | 6 | import com.baeldung.web.exception.CustomException4; |
| 7 | +import com.baeldung.web.exception.CustomException5; |
5 | 8 | import org.springframework.http.HttpStatus; |
6 | | -import org.springframework.http.HttpStatusCode; |
7 | 9 | import org.springframework.http.MediaType; |
8 | 10 | import org.springframework.http.ProblemDetail; |
| 11 | +import org.springframework.http.ResponseEntity; |
9 | 12 | import org.springframework.web.bind.annotation.ExceptionHandler; |
10 | 13 | import org.springframework.web.bind.annotation.ResponseStatus; |
11 | 14 | import org.springframework.web.bind.annotation.RestControllerAdvice; |
12 | 15 |
|
| 16 | +import java.nio.file.AccessDeniedException; |
| 17 | + |
13 | 18 | @RestControllerAdvice |
14 | 19 | public class MyGlobalExceptionHandler { |
15 | 20 |
|
16 | | - // simple example for global exception handling |
17 | 21 | @ResponseStatus(HttpStatus.BAD_REQUEST) |
18 | | - @ExceptionHandler(CustomException3.class) |
19 | | - public void handleCustomException3() { |
| 22 | + @ExceptionHandler(CustomException1.class) |
| 23 | + public void handleException1() { |
20 | 24 | // |
21 | 25 | } |
22 | 26 |
|
23 | | - // content negotiation |
24 | 27 | @ResponseStatus(HttpStatus.BAD_REQUEST) |
25 | | - @ExceptionHandler(produces = MediaType.APPLICATION_JSON_VALUE) |
26 | | - public ProblemDetail handleCustomException4Json(CustomException4 ex) { |
27 | | - String message = "custom exception 4: " + ex.getMessage(); |
| 28 | + @ExceptionHandler |
| 29 | + public ProblemDetail handleException2(CustomException2 ex) { |
28 | 30 | return ProblemDetail |
29 | | - .forStatusAndDetail(HttpStatusCode.valueOf(HttpStatus.BAD_REQUEST.value()), message); |
| 31 | + .forStatusAndDetail( |
| 32 | + HttpStatus.BAD_REQUEST, |
| 33 | + ex.getMessage() |
| 34 | + ); |
30 | 35 | } |
31 | 36 |
|
32 | 37 | @ResponseStatus(HttpStatus.BAD_REQUEST) |
33 | | - @ExceptionHandler(produces = MediaType.TEXT_PLAIN_VALUE) |
34 | | - public String handleCustomException4Text(CustomException4 ex) { |
35 | | - return "custom exception 4: " + ex.getMessage(); |
| 38 | + @ExceptionHandler( produces = MediaType.APPLICATION_JSON_VALUE ) |
| 39 | + public CustomExceptionObject handleException3Json(CustomException3 ex) { |
| 40 | + return new CustomExceptionObject() |
| 41 | + .setMessage("custom exception 3: " + ex.getMessage()); |
| 42 | + } |
| 43 | + |
| 44 | + @ResponseStatus(HttpStatus.BAD_REQUEST) |
| 45 | + @ExceptionHandler( produces = MediaType.TEXT_PLAIN_VALUE ) |
| 46 | + public String handleException3Text(CustomException3 ex) { |
| 47 | + return "custom exception 3: " + ex.getMessage(); |
| 48 | + } |
| 49 | + |
| 50 | + @ResponseStatus(HttpStatus.BAD_REQUEST) |
| 51 | + @ExceptionHandler({ |
| 52 | + CustomException4.class, |
| 53 | + CustomException5.class |
| 54 | + }) |
| 55 | + public ResponseEntity<CustomExceptionObject> handleException45(Exception ex) { |
| 56 | + return ResponseEntity |
| 57 | + .badRequest() |
| 58 | + .body( |
| 59 | + new CustomExceptionObject() |
| 60 | + .setMessage( "custom exception 4/5: " + ex.getMessage()) |
| 61 | + ); |
| 62 | + } |
| 63 | + |
| 64 | + @ResponseStatus(value = HttpStatus.FORBIDDEN) |
| 65 | + @ExceptionHandler( AccessDeniedException.class ) |
| 66 | + public void handleAccessDeniedException() { |
| 67 | + // ... |
36 | 68 | } |
37 | 69 |
|
38 | 70 | } |
0 commit comments