Skip to content

Commit 5537e88

Browse files
committed
Pick up @ExceptionHandler-s from any parent of a class marked as @ControllerAdvice
1 parent c065481 commit 5537e88

File tree

8 files changed

+165
-1
lines changed

8 files changed

+165
-1
lines changed

springdoc-openapi-common/src/main/java/org/springdoc/core/GenericResponseService.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
import org.springframework.core.annotation.AnnotatedElementUtils;
5353
import org.springframework.http.HttpStatus;
5454
import org.springframework.util.CollectionUtils;
55+
import org.springframework.util.ReflectionUtils;
5556
import org.springframework.web.bind.annotation.ExceptionHandler;
5657
import org.springframework.web.bind.annotation.RequestMapping;
5758
import org.springframework.web.bind.annotation.ResponseStatus;
@@ -152,7 +153,7 @@ public void buildGenericResponse(Components components, Map<String, Object> find
152153
if (org.springframework.aop.support.AopUtils.isAopProxy(controllerAdvice))
153154
objClz = org.springframework.aop.support.AopUtils.getTargetClass(controllerAdvice);
154155
ControllerAdviceInfo controllerAdviceInfo = new ControllerAdviceInfo(controllerAdvice);
155-
Arrays.stream(objClz.getDeclaredMethods()).filter(m -> m.isAnnotationPresent(ExceptionHandler.class)).forEach(methods::add);
156+
Arrays.stream(ReflectionUtils.getAllDeclaredMethods(objClz)).filter(m -> m.isAnnotationPresent(ExceptionHandler.class)).forEach(methods::add);
156157
// for each one build ApiResponse and add it to existing responses
157158
for (Method method : methods) {
158159
if (!operationService.isHidden(method)) {
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package test.org.springdoc.api.app157;
2+
3+
import org.springframework.http.HttpStatus;
4+
import org.springframework.web.bind.annotation.ExceptionHandler;
5+
import org.springframework.web.bind.annotation.ResponseStatus;
6+
7+
public class CommonFooErrorHandler {
8+
9+
@ExceptionHandler
10+
@ResponseStatus(HttpStatus.CONFLICT)
11+
public ErrorDTO onException(Exception e) {
12+
return new ErrorDTO("Something wrong has happened");
13+
}
14+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package test.org.springdoc.api.app157;
2+
3+
public class ErrorDTO {
4+
private String message;
5+
6+
public ErrorDTO() {
7+
}
8+
9+
public ErrorDTO(String message) {
10+
this.message = message;
11+
}
12+
13+
public String getMessage() {
14+
return message;
15+
}
16+
17+
public void setMessage(String message) {
18+
this.message = message;
19+
}
20+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package test.org.springdoc.api.app157;
2+
3+
import io.swagger.v3.oas.annotations.OpenAPIDefinition;
4+
import io.swagger.v3.oas.annotations.info.Info;
5+
import io.swagger.v3.oas.annotations.tags.Tag;
6+
import org.springframework.web.bind.annotation.GetMapping;
7+
import org.springframework.web.bind.annotation.RequestMapping;
8+
import org.springframework.web.bind.annotation.RestController;
9+
10+
@RestController
11+
@RequestMapping("/api")
12+
@OpenAPIDefinition(info = @Info(title = "API Examples", version = "1.0"), tags = @Tag(name = "Operations"))
13+
public class HelloController {
14+
15+
@GetMapping("/foo")
16+
public SimpleDTO hello() {
17+
return new SimpleDTO("foo");
18+
}
19+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package test.org.springdoc.api.app157;
2+
3+
public class SimpleDTO {
4+
5+
private String payload;
6+
7+
public SimpleDTO() {
8+
}
9+
10+
public SimpleDTO(String payload) {
11+
this.payload = payload;
12+
}
13+
14+
public String getPayload() {
15+
return payload;
16+
}
17+
18+
public void setPayload(String payload) {
19+
this.payload = payload;
20+
}
21+
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package test.org.springdoc.api.app157;
2+
3+
import org.springframework.web.bind.annotation.ControllerAdvice;
4+
5+
@ControllerAdvice(assignableTypes = HelloController.class)
6+
public class SpecificFooErrorHandler extends CommonFooErrorHandler {
7+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package test.org.springdoc.api.app157;
2+
3+
import org.springframework.boot.autoconfigure.SpringBootApplication;
4+
import test.org.springdoc.api.AbstractSpringDocTest;
5+
6+
public class SpringDocApp157Test extends AbstractSpringDocTest {
7+
8+
@SpringBootApplication
9+
static class SpringDocTestApp {}
10+
11+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
{
2+
"openapi": "3.0.1",
3+
"info": {
4+
"title": "API Examples",
5+
"version": "1.0"
6+
},
7+
"servers": [
8+
{
9+
"url": "http://localhost",
10+
"description": "Generated server url"
11+
}
12+
],
13+
"tags": [
14+
{
15+
"name": "Operations"
16+
}
17+
],
18+
"paths": {
19+
"/api/foo": {
20+
"get": {
21+
"tags": [
22+
"hello-controller"
23+
],
24+
"operationId": "hello",
25+
"responses": {
26+
"409": {
27+
"description": "Conflict",
28+
"content": {
29+
"*/*": {
30+
"schema": {
31+
"$ref": "#/components/schemas/ErrorDTO"
32+
}
33+
}
34+
}
35+
},
36+
"200": {
37+
"description": "OK",
38+
"content": {
39+
"*/*": {
40+
"schema": {
41+
"$ref": "#/components/schemas/SimpleDTO"
42+
}
43+
}
44+
}
45+
}
46+
}
47+
}
48+
}
49+
},
50+
"components": {
51+
"schemas": {
52+
"ErrorDTO": {
53+
"type": "object",
54+
"properties": {
55+
"message": {
56+
"type": "string"
57+
}
58+
}
59+
},
60+
"SimpleDTO": {
61+
"type": "object",
62+
"properties": {
63+
"payload": {
64+
"type": "string"
65+
}
66+
}
67+
}
68+
}
69+
}
70+
}

0 commit comments

Comments
 (0)