Commit 11d59df3 authored by Phillip Webb's avatar Phillip Webb

Add registerErrorPageFilter option flag

Update SpringBootServletInitializer with a registerErrorPageFilter flag
that can be used to disable ErrorPageFilter registration.

Fixes gh-3603
parent de48223a
...@@ -64,6 +64,18 @@ public abstract class SpringBootServletInitializer implements WebApplicationInit ...@@ -64,6 +64,18 @@ public abstract class SpringBootServletInitializer implements WebApplicationInit
protected final Log logger = LogFactory.getLog(getClass()); protected final Log logger = LogFactory.getLog(getClass());
private boolean registerErrorPageFilter = true;
/**
* Set if the {@link ErrorPageFilter} should be registered. Set to {@code false} if
* error page mappings should be handled via the Servlet container and not Spring
* Boot.
* @param registerErrorPageFilter if the {@link ErrorPageFilter} should be registered.
*/
protected final void setRegisterErrorPageFilter(boolean registerErrorPageFilter) {
this.registerErrorPageFilter = registerErrorPageFilter;
}
@Override @Override
public void onStartup(ServletContext servletContext) throws ServletException { public void onStartup(ServletContext servletContext) throws ServletException {
WebApplicationContext rootAppContext = createRootApplicationContext(servletContext); WebApplicationContext rootAppContext = createRootApplicationContext(servletContext);
...@@ -106,7 +118,9 @@ public abstract class SpringBootServletInitializer implements WebApplicationInit ...@@ -106,7 +118,9 @@ public abstract class SpringBootServletInitializer implements WebApplicationInit
"No SpringApplication sources have been defined. Either override the " "No SpringApplication sources have been defined. Either override the "
+ "configure method or add an @Configuration annotation"); + "configure method or add an @Configuration annotation");
// Ensure error pages are registered // Ensure error pages are registered
application.getSources().add(ErrorPageFilter.class); if (this.registerErrorPageFilter) {
application.getSources().add(ErrorPageFilter.class);
}
return run(application); return run(application);
} }
......
...@@ -38,7 +38,7 @@ import static org.hamcrest.Matchers.is; ...@@ -38,7 +38,7 @@ import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat; import static org.junit.Assert.assertThat;
/** /**
* Tests for {@link SpringBootServletInitializerTests}. * Tests for {@link SpringBootServletInitializer}.
* *
* @author Phillip Webb * @author Phillip Webb
* @author Andy Wilkinson * @author Andy Wilkinson
...@@ -75,8 +75,8 @@ public class SpringBootServletInitializerTests { ...@@ -75,8 +75,8 @@ public class SpringBootServletInitializerTests {
equalToSet(Config.class, ErrorPageFilter.class)); equalToSet(Config.class, ErrorPageFilter.class));
} }
@SuppressWarnings("rawtypes")
@Test @Test
@SuppressWarnings("rawtypes")
public void mainClassHasSensibleDefault() throws Exception { public void mainClassHasSensibleDefault() throws Exception {
new WithConfigurationAnnotation() new WithConfigurationAnnotation()
.createRootApplicationContext(this.servletContext); .createRootApplicationContext(this.servletContext);
...@@ -86,6 +86,14 @@ public class SpringBootServletInitializerTests { ...@@ -86,6 +86,14 @@ public class SpringBootServletInitializerTests {
is(equalTo((Class) WithConfigurationAnnotation.class))); is(equalTo((Class) WithConfigurationAnnotation.class)));
} }
@Test
public void withErrorPageFilterNotRegistered() throws Exception {
new WithErrorPageFilterNotRegistered()
.createRootApplicationContext(this.servletContext);
assertThat(this.application.getSources(),
equalToSet(WithErrorPageFilterNotRegistered.class));
}
private Matcher<? super Set<Object>> equalToSet(Object... items) { private Matcher<? super Set<Object>> equalToSet(Object... items) {
Set<Object> set = new LinkedHashSet<Object>(); Set<Object> set = new LinkedHashSet<Object>();
Collections.addAll(set, items); Collections.addAll(set, items);
...@@ -115,6 +123,16 @@ public class SpringBootServletInitializerTests { ...@@ -115,6 +123,16 @@ public class SpringBootServletInitializerTests {
} }
@Configuration
public class WithErrorPageFilterNotRegistered extends
MockSpringBootServletInitializer {
public WithErrorPageFilterNotRegistered() {
setRegisterErrorPageFilter(false);
}
}
@Configuration @Configuration
public static class Config { public static class Config {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment