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
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
public void onStartup(ServletContext servletContext) throws ServletException {
WebApplicationContext rootAppContext = createRootApplicationContext(servletContext);
......@@ -106,7 +118,9 @@ public abstract class SpringBootServletInitializer implements WebApplicationInit
"No SpringApplication sources have been defined. Either override the "
+ "configure method or add an @Configuration annotation");
// Ensure error pages are registered
if (this.registerErrorPageFilter) {
application.getSources().add(ErrorPageFilter.class);
}
return run(application);
}
......
......@@ -38,7 +38,7 @@ import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;
/**
* Tests for {@link SpringBootServletInitializerTests}.
* Tests for {@link SpringBootServletInitializer}.
*
* @author Phillip Webb
* @author Andy Wilkinson
......@@ -75,8 +75,8 @@ public class SpringBootServletInitializerTests {
equalToSet(Config.class, ErrorPageFilter.class));
}
@SuppressWarnings("rawtypes")
@Test
@SuppressWarnings("rawtypes")
public void mainClassHasSensibleDefault() throws Exception {
new WithConfigurationAnnotation()
.createRootApplicationContext(this.servletContext);
......@@ -86,6 +86,14 @@ public class SpringBootServletInitializerTests {
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) {
Set<Object> set = new LinkedHashSet<Object>();
Collections.addAll(set, items);
......@@ -115,6 +123,16 @@ public class SpringBootServletInitializerTests {
}
@Configuration
public class WithErrorPageFilterNotRegistered extends
MockSpringBootServletInitializer {
public WithErrorPageFilterNotRegistered() {
setRegisterErrorPageFilter(false);
}
}
@Configuration
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