Add registerErrorPageFilter option flag
Update SpringBootServletInitializer with a registerErrorPageFilter flag that can be used to disable ErrorPageFilter registration. Fixes gh-3603
This commit is contained in:
parent
de48223a2e
commit
11d59df3fd
|
|
@ -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);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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 {
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue