Spring Download - 404 user page with standalone tomcat

I am running the Spring boot application inside a separate tomcat instance and I am trying to override the error pages. In my opinion, Spring provides an ErrorPageFilter filter that allows me to simply set up error pages, as usual, for the Springs EmbeddedServletContainerCustomizer to handle this case.

So, I have a standard auto configuration / servlet initializer in one class:

 @Configuration @ComponentScan @EnableAutoConfiguration(exclude = [ GroovyTemplateAutoConfiguration, SecurityAutoConfiguration, ErrorMvcAutoConfiguration, JmxAutoConfiguration ] ) class Application extends SpringBootServletInitializer { @Override protected SpringApplicationBuilder configure( SpringApplicationBuilder application ) { application.sources( Application ) } 

(I use the same class for autoconfiguration and servlet init, so I just pass my Application class in the configure method)

Looking at the source code of SpringBootServletInitializer , it looks like the ErrorPageFilter class is ErrorPageFilter added, just extending this class here. I turned off ErrorMvcAutoConfiguration - but then again, looking at this source code, it looks like it's just setting the default error pages, and not setting anything up with ErrorPageFilter .

Then I have an error configuration file:

 @Configuration class ErrorConfiguration implements EmbeddedServletContainerCustomizer { @Override public void customize( ConfigurableEmbeddedServletContainer container ) { container.addErrorPages(new ErrorPage( HttpStatus.NOT_FOUND, "/errors/404" )) } 

However, if I just find an invalid URL, and I DispatcherServlet cannot find a match, then I just get tomcats / 404.html - not my view is associated with " /errors/404 " (I have this path mapped to the timeline view template which works fine - if I go to / errors / 404, it displays fine)

Any ideas why my custom error page is not working? tracking the logs, I get a line about setting ErrorPageFilter and setting ok when the application starts, but then no mention of the filter does anything when the request arrives.

+7
spring spring-boot spring-mvc tomcat
source share
2 answers

You can use the following code for older versions of spring boot (0.5.x)

  public class ServerCustomization extends ServerProperties { @Override public void customize(ConfigurableEmbeddedServletContainerFactory factory) { super.customize(factory); factory.addErrorPages(new ErrorPage(HttpStatus.NOT_FOUND, "/yourpath/error-not-found.jsp")); factory.addErrorPages(new ErrorPage(HttpStatus.INTERNAL_SERVER_ERROR, "/yourpath/error-internal.jsp")); factory.addErrorPages(new ErrorPage("/yourpath/error-other.jsp")); } } 

Newer versions of spring for download (1.X.RELEASE) have some refactoring around ServerProperties. See below,

  public class ServerCustomization extends ServerProperties { @Override public void customize(ConfigurableEmbeddedServletContainer container) { super.customize(container); container.addErrorPages(new ErrorPage(HttpStatus.NOT_FOUND, "/jsp/404.jsp")); container.addErrorPages(new ErrorPage(HttpStatus.INTERNAL_SERVER_ERROR, "/jsp/500.jsp")); container.addErrorPages(new ErrorPage("/jsp/error.jsp")); } } 

Then define a bean to enter ServerProperies.

 @Bean public ServerProperties getServerProperties() { return new ServerCustomization(); } 

Sample project published in git

Very important: if you use maven for assembly, you must save all resource files in the src / main / resources folder. Otherwise, maven will not add these files to the last banner artifact.

+7
source share

You can use the Spring view of the built-in boot error by implementing a view called error or by disabling it by setting the error.whitelabel.enabled=false property and implementing your own. This is explained more in the docs .

+2
source share

All Articles