Organizational Research By

Surprising Reserch Topic

spring boot custom 404 page with standalone tomcat

spring boot custom 404 page with standalone tomcat  using -'spring,spring-mvc,tomcat,spring-boot'

I am running a Spring boot application inside a standalone tomcat instance, and I am trying to override the error pages. From my understanding, Spring provides a filter ErrorPageFilter that allows me to just setup error pages as normal for Springs EmbeddedServletContainerCustomizer  to handle this case exactly.

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

@EnableAutoConfiguration(exclude = [ GroovyTemplateAutoConfiguration, SecurityAutoConfiguration, ErrorMvcAutoConfiguration, JmxAutoConfiguration ] )
class Application extends SpringBootServletInitializer {

    @Override protected SpringApplicationBuilder configure( SpringApplicationBuilder application ) {
        application.sources( Application )

(I am using the same class for autoconfiguration and servlet init, which is why i just pass my Application class in the configure method)

Looking at the source code for SpringBootServletInitializer it looks like the ErrorPageFilter class is being added by just extending that class here.  I have turned off the ErrorMvcAutoConfiguration - but again, looking at that source code it looks like that is just setting default error pages and not actually setting anything up with the ErrorPageFilter.

I then have my error config file:

class ErrorConfiguration implements EmbeddedServletContainerCustomizer {

    @Override public void customize( ConfigurableEmbeddedServletContainer container ) {
        container.addErrorPages(new ErrorPage( HttpStatus.NOT_FOUND, "/errors/404" ))

However, if I just visit an invalid URL, and I DispatcherServlet can't find a match then I just get tomcats /404.html - not my view linked to "/errors/404"  (I have this path mapped to a thymeleaf view template, that works fine - if I navigate to /errors/404 it displays ok)

Any ideas why my custom error page is not working?  tracing the logs, I get a line about the ErrorPageFilter being configured and setup ok on application startup, but then no mentions of the filter doing anything when a request comes in.

asked Sep 14, 2015 by Chr8975
0 votes

Related Hot Questions

2 Answers

0 votes
answered Sep 14, 2015 by CorCarswell
0 votes
answered Sep 14, 2015 by EliNapier