Skip to content

Commit 11d59df

Browse files
committed
Add registerErrorPageFilter option flag
Update SpringBootServletInitializer with a registerErrorPageFilter flag that can be used to disable ErrorPageFilter registration. Fixes gh-3603
1 parent de48223 commit 11d59df

File tree

2 files changed

+35
-3
lines changed

2 files changed

+35
-3
lines changed

spring-boot/src/main/java/org/springframework/boot/context/web/SpringBootServletInitializer.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,18 @@ public abstract class SpringBootServletInitializer implements WebApplicationInit
6464

6565
protected final Log logger = LogFactory.getLog(getClass());
6666

67+
private boolean registerErrorPageFilter = true;
68+
69+
/**
70+
* Set if the {@link ErrorPageFilter} should be registered. Set to {@code false} if
71+
* error page mappings should be handled via the Servlet container and not Spring
72+
* Boot.
73+
* @param registerErrorPageFilter if the {@link ErrorPageFilter} should be registered.
74+
*/
75+
protected final void setRegisterErrorPageFilter(boolean registerErrorPageFilter) {
76+
this.registerErrorPageFilter = registerErrorPageFilter;
77+
}
78+
6779
@Override
6880
public void onStartup(ServletContext servletContext) throws ServletException {
6981
WebApplicationContext rootAppContext = createRootApplicationContext(servletContext);
@@ -106,7 +118,9 @@ protected WebApplicationContext createRootApplicationContext(
106118
"No SpringApplication sources have been defined. Either override the "
107119
+ "configure method or add an @Configuration annotation");
108120
// Ensure error pages are registered
109-
application.getSources().add(ErrorPageFilter.class);
121+
if (this.registerErrorPageFilter) {
122+
application.getSources().add(ErrorPageFilter.class);
123+
}
110124
return run(application);
111125
}
112126

spring-boot/src/test/java/org/springframework/boot/context/web/SpringBootServletInitializerTests.java

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
import static org.junit.Assert.assertThat;
3939

4040
/**
41-
* Tests for {@link SpringBootServletInitializerTests}.
41+
* Tests for {@link SpringBootServletInitializer}.
4242
*
4343
* @author Phillip Webb
4444
* @author Andy Wilkinson
@@ -75,8 +75,8 @@ public void withConfiguredSource() throws Exception {
7575
equalToSet(Config.class, ErrorPageFilter.class));
7676
}
7777

78-
@SuppressWarnings("rawtypes")
7978
@Test
79+
@SuppressWarnings("rawtypes")
8080
public void mainClassHasSensibleDefault() throws Exception {
8181
new WithConfigurationAnnotation()
8282
.createRootApplicationContext(this.servletContext);
@@ -86,6 +86,14 @@ public void mainClassHasSensibleDefault() throws Exception {
8686
is(equalTo((Class) WithConfigurationAnnotation.class)));
8787
}
8888

89+
@Test
90+
public void withErrorPageFilterNotRegistered() throws Exception {
91+
new WithErrorPageFilterNotRegistered()
92+
.createRootApplicationContext(this.servletContext);
93+
assertThat(this.application.getSources(),
94+
equalToSet(WithErrorPageFilterNotRegistered.class));
95+
}
96+
8997
private Matcher<? super Set<Object>> equalToSet(Object... items) {
9098
Set<Object> set = new LinkedHashSet<Object>();
9199
Collections.addAll(set, items);
@@ -115,6 +123,16 @@ protected SpringApplicationBuilder configure(SpringApplicationBuilder applicatio
115123

116124
}
117125

126+
@Configuration
127+
public class WithErrorPageFilterNotRegistered extends
128+
MockSpringBootServletInitializer {
129+
130+
public WithErrorPageFilterNotRegistered() {
131+
setRegisterErrorPageFilter(false);
132+
}
133+
134+
}
135+
118136
@Configuration
119137
public static class Config {
120138

0 commit comments

Comments
 (0)