Skip to content

Commit 7017e10

Browse files
committed
Merge pull request #31495 from terminux
* pr/31495: Polish CompositeHandlerExceptionResolver Use ExceptionHandler when Spring MVC uses a different management port Closes gh-31495
2 parents a784156 + aed4c47 commit 7017e10

File tree

3 files changed

+143
-16
lines changed

3 files changed

+143
-16
lines changed

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

Lines changed: 35 additions & 16 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.
@@ -18,11 +18,12 @@
1818

1919
import java.util.ArrayList;
2020
import java.util.List;
21-
import java.util.Objects;
2221

2322
import javax.servlet.http.HttpServletRequest;
2423
import javax.servlet.http.HttpServletResponse;
2524

25+
import org.springframework.beans.factory.BeanFactory;
26+
import org.springframework.beans.factory.HierarchicalBeanFactory;
2627
import org.springframework.beans.factory.ListableBeanFactory;
2728
import org.springframework.beans.factory.annotation.Autowired;
2829
import org.springframework.boot.web.servlet.error.DefaultErrorAttributes;
@@ -38,34 +39,52 @@
3839
* @author Stephane Nicoll
3940
* @author Phillip Webb
4041
* @author Scott Frederick
42+
* @author Guirong Hu
4143
*/
4244
class CompositeHandlerExceptionResolver implements HandlerExceptionResolver {
4345

4446
@Autowired
4547
private ListableBeanFactory beanFactory;
4648

47-
private List<HandlerExceptionResolver> resolvers;
49+
private transient List<HandlerExceptionResolver> resolvers;
4850

4951
@Override
5052
public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler,
5153
Exception ex) {
52-
if (this.resolvers == null) {
53-
this.resolvers = extractResolvers();
54+
for (HandlerExceptionResolver resolver : getResolvers()) {
55+
ModelAndView resolved = resolver.resolveException(request, response, handler, ex);
56+
if (resolved != null) {
57+
return resolved;
58+
}
5459
}
55-
return this.resolvers.stream().map((resolver) -> resolver.resolveException(request, response, handler, ex))
56-
.filter(Objects::nonNull).findFirst().orElse(null);
60+
return null;
5761
}
5862

59-
private List<HandlerExceptionResolver> extractResolvers() {
60-
List<HandlerExceptionResolver> list = new ArrayList<>(
61-
this.beanFactory.getBeansOfType(HandlerExceptionResolver.class).values());
62-
list.remove(this);
63-
AnnotationAwareOrderComparator.sort(list);
64-
if (list.isEmpty()) {
65-
list.add(new DefaultErrorAttributes());
66-
list.add(new DefaultHandlerExceptionResolver());
63+
private List<HandlerExceptionResolver> getResolvers() {
64+
List<HandlerExceptionResolver> resolvers = this.resolvers;
65+
if (resolvers == null) {
66+
resolvers = new ArrayList<>();
67+
collectResolverBeans(resolvers, this.beanFactory);
68+
resolvers.remove(this);
69+
AnnotationAwareOrderComparator.sort(resolvers);
70+
if (resolvers.isEmpty()) {
71+
resolvers.add(new DefaultErrorAttributes());
72+
resolvers.add(new DefaultHandlerExceptionResolver());
73+
}
74+
this.resolvers = resolvers;
75+
}
76+
return resolvers;
77+
}
78+
79+
private void collectResolverBeans(List<HandlerExceptionResolver> resolvers, BeanFactory beanFactory) {
80+
if (beanFactory instanceof ListableBeanFactory) {
81+
ListableBeanFactory listableBeanFactory = (ListableBeanFactory) beanFactory;
82+
resolvers.addAll(listableBeanFactory.getBeansOfType(HandlerExceptionResolver.class).values());
83+
}
84+
if (beanFactory instanceof HierarchicalBeanFactory) {
85+
HierarchicalBeanFactory hierarchicalBeanFactory = (HierarchicalBeanFactory) beanFactory;
86+
collectResolverBeans(resolvers, hierarchicalBeanFactory.getParentBeanFactory());
6787
}
68-
return list;
6988
}
7089

7190
}
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)