Skip to content

Commit 3592292

Browse files
terminuxphilwebb
authored andcommitted
Use ExceptionHandler when Spring MVC uses a different management port
Update `CompositeHandlerExceptionResolver` to search for beans in all contexts. Note that `BeanFactoryUtils.beansOfTypeIncludingAncestors` cannot not be used since we need to pick up all beans, even if they have the same name. See gh-31495
1 parent a784156 commit 3592292

File tree

3 files changed

+122
-3
lines changed

3 files changed

+122
-3
lines changed

spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/web/servlet/CompositeHandlerExceptionResolver.java

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2020 the original author or authors.
2+
* Copyright 2012-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -23,6 +23,8 @@
2323
import javax.servlet.http.HttpServletRequest;
2424
import javax.servlet.http.HttpServletResponse;
2525

26+
import org.springframework.beans.factory.BeanFactory;
27+
import org.springframework.beans.factory.HierarchicalBeanFactory;
2628
import org.springframework.beans.factory.ListableBeanFactory;
2729
import org.springframework.beans.factory.annotation.Autowired;
2830
import org.springframework.boot.web.servlet.error.DefaultErrorAttributes;
@@ -38,6 +40,7 @@
3840
* @author Stephane Nicoll
3941
* @author Phillip Webb
4042
* @author Scott Frederick
43+
* @author Guirong Hu
4144
*/
4245
class CompositeHandlerExceptionResolver implements HandlerExceptionResolver {
4346

@@ -57,8 +60,16 @@ public ModelAndView resolveException(HttpServletRequest request, HttpServletResp
5760
}
5861

5962
private List<HandlerExceptionResolver> extractResolvers() {
60-
List<HandlerExceptionResolver> list = new ArrayList<>(
61-
this.beanFactory.getBeansOfType(HandlerExceptionResolver.class).values());
63+
List<HandlerExceptionResolver> list = new ArrayList<>();
64+
BeanFactory beanFactory = this.beanFactory;
65+
while (beanFactory != null) {
66+
if (beanFactory instanceof ListableBeanFactory) {
67+
list.addAll(
68+
((ListableBeanFactory) beanFactory).getBeansOfType(HandlerExceptionResolver.class).values());
69+
}
70+
beanFactory = (beanFactory instanceof HierarchicalBeanFactory)
71+
? ((HierarchicalBeanFactory) beanFactory).getParentBeanFactory() : null;
72+
}
6273
list.remove(this);
6374
AnnotationAwareOrderComparator.sort(list);
6475
if (list.isEmpty()) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* Copyright 2012-2022 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package smoketest.actuator;
18+
19+
import org.springframework.boot.actuate.endpoint.web.annotation.RestControllerEndpoint;
20+
import org.springframework.http.HttpStatus;
21+
import org.springframework.http.ResponseEntity;
22+
import org.springframework.stereotype.Component;
23+
import org.springframework.web.bind.annotation.ExceptionHandler;
24+
import org.springframework.web.bind.annotation.GetMapping;
25+
import org.springframework.web.bind.annotation.RestControllerAdvice;
26+
27+
/**
28+
* The Sample rest controller endpoint with exception.
29+
*
30+
* @author Guirong Hu
31+
*/
32+
@Component
33+
@RestControllerEndpoint(id = "exception")
34+
public class SampleRestControllerEndpointWithException {
35+
36+
@GetMapping("/")
37+
public String exception() {
38+
throw new CustomException();
39+
}
40+
41+
@RestControllerAdvice
42+
static class CustomExceptionHandler {
43+
44+
@ExceptionHandler(CustomException.class)
45+
ResponseEntity<String> handleCustomException(CustomException e) {
46+
return new ResponseEntity<>("this is a custom exception body", HttpStatus.I_AM_A_TEAPOT);
47+
}
48+
49+
}
50+
51+
static class CustomException extends RuntimeException {
52+
53+
}
54+
55+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* Copyright 2012-2022 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package smoketest.actuator;
18+
19+
import org.junit.jupiter.api.Test;
20+
21+
import org.springframework.boot.actuate.autoconfigure.web.server.LocalManagementPort;
22+
import org.springframework.boot.actuate.endpoint.web.annotation.RestControllerEndpoint;
23+
import org.springframework.boot.test.context.SpringBootTest;
24+
import org.springframework.boot.test.web.client.TestRestTemplate;
25+
import org.springframework.http.HttpStatus;
26+
import org.springframework.http.ResponseEntity;
27+
import org.springframework.web.bind.annotation.ExceptionHandler;
28+
29+
import static org.assertj.core.api.Assertions.assertThat;
30+
31+
/**
32+
* Integration tests for separate management and main service ports with Actuator's MVC
33+
* {@link RestControllerEndpoint rest controller endpoints} and {@link ExceptionHandler
34+
* exception handler}.
35+
*
36+
* @author Guirong Hu
37+
*/
38+
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT,
39+
properties = { "management.endpoints.web.exposure.include=*", "management.server.port=0" })
40+
class ManagementDifferentPortAndEndpointWithExceptionHandlerSampleActuatorApplicationTests {
41+
42+
@LocalManagementPort
43+
private int managementPort;
44+
45+
@Test
46+
void testExceptionHandlerRestControllerEndpoint() {
47+
ResponseEntity<String> entity = new TestRestTemplate("user", "password")
48+
.getForEntity("http://localhost:" + this.managementPort + "/actuator/exception", String.class);
49+
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.I_AM_A_TEAPOT);
50+
assertThat(entity.getBody()).isEqualTo("this is a custom exception body");
51+
}
52+
53+
}

0 commit comments

Comments
 (0)