Skip to content

Commit 215fa5f

Browse files
author
bnasslahsen
committed
Add support for @ExceptionHandler inside @RestController. Fixes #748.
1 parent 71c2614 commit 215fa5f

File tree

5 files changed

+191
-1
lines changed

5 files changed

+191
-1
lines changed

springdoc-openapi-common/src/main/java/org/springdoc/api/AbstractOpenApiResource.java

+4-1
Original file line numberDiff line numberDiff line change
@@ -246,8 +246,11 @@ protected synchronized OpenAPI getOpenApi() {
246246
Map<String, Object> findControllerAdvice = openAPIBuilder.getControllerAdviceMap();
247247
// calculate generic responses
248248
openApi = openAPIBuilder.getCalculatedOpenAPI();
249-
if (springDocConfigProperties.isOverrideWithGenericResponse())
249+
if (springDocConfigProperties.isOverrideWithGenericResponse() && !CollectionUtils.isEmpty(findControllerAdvice)){
250+
if(!CollectionUtils.isEmpty(mappingsMap))
251+
findControllerAdvice.putAll(mappingsMap);
250252
responseBuilder.buildGenericResponse(openApi.getComponents(), findControllerAdvice);
253+
}
251254
getPaths(mappingsMap);
252255
// run the optional customisers
253256
openApiCustomisers.ifPresent(apiCustomisers -> apiCustomisers.forEach(openApiCustomiser -> openApiCustomiser.customise(openApi)));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
*
3+
* * Copyright 2019-2020 the original author or authors.
4+
* *
5+
* * Licensed under the Apache License, Version 2.0 (the "License");
6+
* * you may not use this file except in compliance with the License.
7+
* * You may obtain a copy of the License at
8+
* *
9+
* * https://www.apache.org/licenses/LICENSE-2.0
10+
* *
11+
* * Unless required by applicable law or agreed to in writing, software
12+
* * distributed under the License is distributed on an "AS IS" BASIS,
13+
* * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* * See the License for the specific language governing permissions and
15+
* * limitations under the License.
16+
*
17+
*/
18+
19+
package test.org.springdoc.api.app124;
20+
21+
import io.swagger.v3.oas.annotations.Operation;
22+
import io.swagger.v3.oas.annotations.media.Content;
23+
import io.swagger.v3.oas.annotations.responses.ApiResponse;
24+
25+
import org.springframework.http.HttpStatus;
26+
import org.springframework.web.bind.annotation.ExceptionHandler;
27+
import org.springframework.web.bind.annotation.GetMapping;
28+
import org.springframework.web.bind.annotation.PathVariable;
29+
import org.springframework.web.bind.annotation.ResponseStatus;
30+
import org.springframework.web.bind.annotation.RestController;
31+
32+
@RestController
33+
public class HelloController<T> {
34+
35+
@ExceptionHandler(IllegalArgumentException.class)
36+
@ApiResponse(responseCode = "404", description = "Not here", content = @Content)
37+
@ResponseStatus(HttpStatus.NOT_FOUND)
38+
public void bad(IllegalArgumentException e) {
39+
40+
}
41+
42+
@GetMapping(value = "/hello/{numTelco}")
43+
@Operation(summary = "GET Persons", responses = @ApiResponse(responseCode = "418"))
44+
public T index(@PathVariable("numTelco") String numTel, String adresse) {
45+
throw new IllegalArgumentException();
46+
47+
}
48+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
*
3+
* * Copyright 2019-2020 the original author or authors.
4+
* *
5+
* * Licensed under the Apache License, Version 2.0 (the "License");
6+
* * you may not use this file except in compliance with the License.
7+
* * You may obtain a copy of the License at
8+
* *
9+
* * https://www.apache.org/licenses/LICENSE-2.0
10+
* *
11+
* * Unless required by applicable law or agreed to in writing, software
12+
* * distributed under the License is distributed on an "AS IS" BASIS,
13+
* * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* * See the License for the specific language governing permissions and
15+
* * limitations under the License.
16+
*
17+
*/
18+
19+
package test.org.springdoc.api.app124;
20+
21+
import org.springframework.http.HttpStatus;
22+
import org.springframework.web.bind.annotation.ExceptionHandler;
23+
import org.springframework.web.bind.annotation.ResponseStatus;
24+
import org.springframework.web.bind.annotation.RestControllerAdvice;
25+
26+
@RestControllerAdvice
27+
public class MyExceptionHandler {
28+
29+
@ExceptionHandler(RuntimeException.class)
30+
@ResponseStatus(HttpStatus.BAD_GATEWAY)
31+
public Object gateway(RuntimeException e) {
32+
return null;
33+
}
34+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
*
3+
* *
4+
* * *
5+
* * * * Copyright 2019-2020 the original author or authors.
6+
* * * *
7+
* * * * Licensed under the Apache License, Version 2.0 (the "License");
8+
* * * * you may not use this file except in compliance with the License.
9+
* * * * You may obtain a copy of the License at
10+
* * * *
11+
* * * * https://www.apache.org/licenses/LICENSE-2.0
12+
* * * *
13+
* * * * Unless required by applicable law or agreed to in writing, software
14+
* * * * distributed under the License is distributed on an "AS IS" BASIS,
15+
* * * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* * * * See the License for the specific language governing permissions and
17+
* * * * limitations under the License.
18+
* * *
19+
* *
20+
*
21+
*
22+
*/
23+
package test.org.springdoc.api.app124;
24+
25+
import test.org.springdoc.api.AbstractSpringDocTest;
26+
27+
import org.springframework.boot.autoconfigure.SpringBootApplication;
28+
29+
30+
/**
31+
* Tests Spring meta-annotations as method parameters
32+
*/
33+
public class SpringDocApp124Test extends AbstractSpringDocTest {
34+
35+
@SpringBootApplication
36+
static class SpringDocTestApp {}
37+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
{
2+
"openapi": "3.0.1",
3+
"info": {
4+
"title": "OpenAPI definition",
5+
"version": "v0"
6+
},
7+
"servers": [
8+
{
9+
"url": "http://localhost",
10+
"description": "Generated server url"
11+
}
12+
],
13+
"paths": {
14+
"/hello/{numTelco}": {
15+
"get": {
16+
"tags": [
17+
"hello-controller"
18+
],
19+
"summary": "GET Persons",
20+
"operationId": "index",
21+
"parameters": [
22+
{
23+
"name": "numTelco",
24+
"in": "path",
25+
"required": true,
26+
"schema": {
27+
"type": "string"
28+
}
29+
},
30+
{
31+
"name": "adresse",
32+
"in": "query",
33+
"required": true,
34+
"schema": {
35+
"type": "string"
36+
}
37+
}
38+
],
39+
"responses": {
40+
"502": {
41+
"description": "Bad Gateway",
42+
"content": {
43+
"*/*": {
44+
"schema": {
45+
"type": "object"
46+
}
47+
}
48+
}
49+
},
50+
"404": {
51+
"description": "Not here"
52+
},
53+
"418": {
54+
"description": "I'm a teapot",
55+
"content": {
56+
"*/*": {
57+
"schema": {
58+
"type": "object"
59+
}
60+
}
61+
}
62+
}
63+
}
64+
}
65+
}
66+
},
67+
"components": {}
68+
}

0 commit comments

Comments
 (0)