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
This commit is contained in:
Andy Wilkinson 2015-10-14 12:06:10 +01:00
parent 9218d5ad11
commit e4895f8fde
2 changed files with 25 additions and 7 deletions

View File

@ -75,9 +75,6 @@ public class EndpointWebMvcChildContextConfiguration {
@Value("${error.path:/error}")
private String errorPath = "/error";
@Autowired(required = false)
private List<EndpointHandlerMappingCustomizer> mappingCustomizers;
@Configuration
protected static class ServerCustomization
implements EmbeddedServletContainerCustomizer, Ordered {
@ -144,11 +141,12 @@ public class EndpointWebMvcChildContextConfiguration {
/*
* The error controller is present but not mapped as an endpoint in this context
* because of the DispatcherServlet having had it's HandlerMapping explicitly
* disabled. So we expose the same feature but only for machine endpoints.
* because of the DispatcherServlet having had its HandlerMapping explicitly disabled.
* So we expose the same feature but only for machine endpoints.
*/
@Bean
public ManagementErrorEndpoint errorEndpoint(final ErrorAttributes errorAttributes) {
@ConditionalOnBean(ErrorAttributes.class)
public ManagementErrorEndpoint errorEndpoint(ErrorAttributes errorAttributes) {
return new ManagementErrorEndpoint(this.errorPath, errorAttributes);
}

View File

@ -27,6 +27,7 @@ import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.hamcrest.Matcher;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
@ -76,6 +77,7 @@ import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.notNullValue;
import static org.hamcrest.Matchers.startsWith;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertThat;
@ -146,6 +148,7 @@ public class EndpointWebMvcAutoConfigurationTests {
assertContent("/endpoint", ports.get().server, null);
assertContent("/controller", ports.get().management, null);
assertContent("/endpoint", ports.get().management, "endpointoutput");
assertContent("/error", ports.get().management, startsWith("{\"timestamp\""));
List<?> interceptors = (List<?>) ReflectionTestUtils.getField(
this.applicationContext.getBean(EndpointHandlerMapping.class),
"interceptors");
@ -154,6 +157,17 @@ public class EndpointWebMvcAutoConfigurationTests {
assertAllClosed();
}
@Test
public void onDifferentPortWithoutErrorMvcAutoConfiguration() throws Exception {
this.applicationContext.register(RootConfig.class, EndpointConfig.class,
DifferentPortConfig.class, BaseConfiguration.class,
EndpointWebMvcAutoConfiguration.class);
this.applicationContext.refresh();
assertContent("/error", ports.get().management, null);
this.applicationContext.close();
assertAllClosed();
}
@Test
public void onDifferentPortInServletContainer() throws Exception {
this.applicationContext.register(RootConfig.class, EndpointConfig.class,
@ -378,6 +392,7 @@ public class EndpointWebMvcAutoConfigurationTests {
assertContent("/endpoint", ports.get().management, null);
}
@SuppressWarnings("unchecked")
public void assertContent(String url, int port, Object expected) throws Exception {
SimpleClientHttpRequestFactory clientHttpRequestFactory = new SimpleClientHttpRequestFactory();
ClientHttpRequest request = clientHttpRequestFactory
@ -387,7 +402,12 @@ public class EndpointWebMvcAutoConfigurationTests {
try {
String actual = StreamUtils.copyToString(response.getBody(),
Charset.forName("UTF-8"));
assertThat(actual, equalTo(expected));
if (expected instanceof Matcher) {
assertThat(actual, is((Matcher<String>) expected));
}
else {
assertThat(actual, equalTo(expected));
}
}
finally {
response.close();