Skip to content

Commit 6306919

Browse files
authored
Co-authored-by: Ralf Ueberfuhr <[email protected]>
1 parent 3b4d7d8 commit 6306919

File tree

3 files changed

+73
-12
lines changed

3 files changed

+73
-12
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.baeldung.web.error;
2+
3+
public class CustomExceptionObject {
4+
5+
private String message;
6+
7+
public String getMessage() {
8+
return message;
9+
}
10+
11+
public CustomExceptionObject setMessage(String message) {
12+
this.message = message;
13+
return this;
14+
}
15+
16+
}
Lines changed: 44 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,70 @@
11
package com.baeldung.web.error;
22

3+
import com.baeldung.web.exception.CustomException1;
4+
import com.baeldung.web.exception.CustomException2;
35
import com.baeldung.web.exception.CustomException3;
46
import com.baeldung.web.exception.CustomException4;
7+
import com.baeldung.web.exception.CustomException5;
58
import org.springframework.http.HttpStatus;
6-
import org.springframework.http.HttpStatusCode;
79
import org.springframework.http.MediaType;
810
import org.springframework.http.ProblemDetail;
11+
import org.springframework.http.ResponseEntity;
912
import org.springframework.web.bind.annotation.ExceptionHandler;
1013
import org.springframework.web.bind.annotation.ResponseStatus;
1114
import org.springframework.web.bind.annotation.RestControllerAdvice;
1215

16+
import java.nio.file.AccessDeniedException;
17+
1318
@RestControllerAdvice
1419
public class MyGlobalExceptionHandler {
1520

16-
// simple example for global exception handling
1721
@ResponseStatus(HttpStatus.BAD_REQUEST)
18-
@ExceptionHandler(CustomException3.class)
19-
public void handleCustomException3() {
22+
@ExceptionHandler(CustomException1.class)
23+
public void handleException1() {
2024
//
2125
}
2226

23-
// content negotiation
2427
@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) {
2830
return ProblemDetail
29-
.forStatusAndDetail(HttpStatusCode.valueOf(HttpStatus.BAD_REQUEST.value()), message);
31+
.forStatusAndDetail(
32+
HttpStatus.BAD_REQUEST,
33+
ex.getMessage()
34+
);
3035
}
3136

3237
@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+
// ...
3668
}
3769

3870
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.baeldung.web.exception;
2+
3+
import java.io.Serial;
4+
5+
public class CustomException5 extends RuntimeException {
6+
7+
@Serial
8+
private static final long serialVersionUID = 1L;
9+
10+
public CustomException5(String message) {
11+
super(message);
12+
}
13+
}

0 commit comments

Comments
 (0)