diff --git a/spring-boot/src/main/java/org/springframework/boot/context/web/SpringBootServletInitializer.java b/spring-boot/src/main/java/org/springframework/boot/context/web/SpringBootServletInitializer.java index 3e711be5bdb..f30897780e4 100644 --- a/spring-boot/src/main/java/org/springframework/boot/context/web/SpringBootServletInitializer.java +++ b/spring-boot/src/main/java/org/springframework/boot/context/web/SpringBootServletInitializer.java @@ -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 - application.getSources().add(ErrorPageFilter.class); + if (this.registerErrorPageFilter) { + application.getSources().add(ErrorPageFilter.class); + } return run(application); } diff --git a/spring-boot/src/test/java/org/springframework/boot/context/web/SpringBootServletInitializerTests.java b/spring-boot/src/test/java/org/springframework/boot/context/web/SpringBootServletInitializerTests.java index ddd796b6bef..643f970eb36 100644 --- a/spring-boot/src/test/java/org/springframework/boot/context/web/SpringBootServletInitializerTests.java +++ b/spring-boot/src/test/java/org/springframework/boot/context/web/SpringBootServletInitializerTests.java @@ -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> equalToSet(Object... items) { Set set = new LinkedHashSet(); 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 {