Skip to content

Commit e4895f8

Browse files
committed
Disable child context's /error endpoint if disabled in parent
Previously, EndpointWebMvcChildContextConfiguration would attempt to create a /error endpoint, irrespective of whether or not the parent had such an endpoint. If the endpoint was disabled in the parent this would cause a failure due to the absence of an ErrorAttributes bean. This commit updates EndpointWebMvcChildContextConfiguration to make the creation of its /error endpoint conditional on the existence of an ErrorAttributes bean. Closes gh-4164
1 parent 9218d5a commit e4895f8

File tree

2 files changed

+25
-7
lines changed

2 files changed

+25
-7
lines changed

spring-boot-actuator/src/main/java/org/springframework/boot/actuate/autoconfigure/EndpointWebMvcChildContextConfiguration.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,6 @@ public class EndpointWebMvcChildContextConfiguration {
7575
@Value("${error.path:/error}")
7676
private String errorPath = "/error";
7777

78-
@Autowired(required = false)
79-
private List<EndpointHandlerMappingCustomizer> mappingCustomizers;
80-
8178
@Configuration
8279
protected static class ServerCustomization
8380
implements EmbeddedServletContainerCustomizer, Ordered {
@@ -144,11 +141,12 @@ public HandlerAdapter handlerAdapter(HttpMessageConverters converters) {
144141

145142
/*
146143
* The error controller is present but not mapped as an endpoint in this context
147-
* because of the DispatcherServlet having had it's HandlerMapping explicitly
148-
* disabled. So we expose the same feature but only for machine endpoints.
144+
* because of the DispatcherServlet having had its HandlerMapping explicitly disabled.
145+
* So we expose the same feature but only for machine endpoints.
149146
*/
150147
@Bean
151-
public ManagementErrorEndpoint errorEndpoint(final ErrorAttributes errorAttributes) {
148+
@ConditionalOnBean(ErrorAttributes.class)
149+
public ManagementErrorEndpoint errorEndpoint(ErrorAttributes errorAttributes) {
152150
return new ManagementErrorEndpoint(this.errorPath, errorAttributes);
153151
}
154152

spring-boot-actuator/src/test/java/org/springframework/boot/actuate/autoconfigure/EndpointWebMvcAutoConfigurationTests.java

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import javax.servlet.http.HttpServletRequest;
2828
import javax.servlet.http.HttpServletResponse;
2929

30+
import org.hamcrest.Matcher;
3031
import org.junit.After;
3132
import org.junit.Before;
3233
import org.junit.Test;
@@ -76,6 +77,7 @@
7677
import static org.hamcrest.Matchers.is;
7778
import static org.hamcrest.Matchers.not;
7879
import static org.hamcrest.Matchers.notNullValue;
80+
import static org.hamcrest.Matchers.startsWith;
7981
import static org.junit.Assert.assertEquals;
8082
import static org.junit.Assert.assertFalse;
8183
import static org.junit.Assert.assertThat;
@@ -146,6 +148,7 @@ public void onDifferentPort() throws Exception {
146148
assertContent("/endpoint", ports.get().server, null);
147149
assertContent("/controller", ports.get().management, null);
148150
assertContent("/endpoint", ports.get().management, "endpointoutput");
151+
assertContent("/error", ports.get().management, startsWith("{\"timestamp\""));
149152
List<?> interceptors = (List<?>) ReflectionTestUtils.getField(
150153
this.applicationContext.getBean(EndpointHandlerMapping.class),
151154
"interceptors");
@@ -154,6 +157,17 @@ public void onDifferentPort() throws Exception {
154157
assertAllClosed();
155158
}
156159

160+
@Test
161+
public void onDifferentPortWithoutErrorMvcAutoConfiguration() throws Exception {
162+
this.applicationContext.register(RootConfig.class, EndpointConfig.class,
163+
DifferentPortConfig.class, BaseConfiguration.class,
164+
EndpointWebMvcAutoConfiguration.class);
165+
this.applicationContext.refresh();
166+
assertContent("/error", ports.get().management, null);
167+
this.applicationContext.close();
168+
assertAllClosed();
169+
}
170+
157171
@Test
158172
public void onDifferentPortInServletContainer() throws Exception {
159173
this.applicationContext.register(RootConfig.class, EndpointConfig.class,
@@ -378,6 +392,7 @@ private void assertAllClosed() throws Exception {
378392
assertContent("/endpoint", ports.get().management, null);
379393
}
380394

395+
@SuppressWarnings("unchecked")
381396
public void assertContent(String url, int port, Object expected) throws Exception {
382397
SimpleClientHttpRequestFactory clientHttpRequestFactory = new SimpleClientHttpRequestFactory();
383398
ClientHttpRequest request = clientHttpRequestFactory
@@ -387,7 +402,12 @@ public void assertContent(String url, int port, Object expected) throws Exceptio
387402
try {
388403
String actual = StreamUtils.copyToString(response.getBody(),
389404
Charset.forName("UTF-8"));
390-
assertThat(actual, equalTo(expected));
405+
if (expected instanceof Matcher) {
406+
assertThat(actual, is((Matcher<String>) expected));
407+
}
408+
else {
409+
assertThat(actual, equalTo(expected));
410+
}
391411
}
392412
finally {
393413
response.close();

0 commit comments

Comments
 (0)