title: Spring Boot - custom 404 page with standalone tomcat
body: <p>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 <a href="https://github.com/spring-projects/spring-boot/blob/master/spring-boot/src/main/java/org/springframework/boot/context/web/ErrorPageFilter.java">ErrorPageFilter</a> that allows me to just setup error pages as normal for Springs <code>EmbeddedServletContainerCustomizer</code>  to handle this case exactly.</p>
  
  <p>So I have my standard auto configuration/servlet initializer in one class:</p>
  
  <pre><code>@Configuration
  @ComponentScan
  @EnableAutoConfiguration(exclude = [ GroovyTemplateAutoConfiguration, SecurityAutoConfiguration, ErrorMvcAutoConfiguration, JmxAutoConfiguration ] )
  class Application extends SpringBootServletInitializer {
  
      @Override protected SpringApplicationBuilder configure( SpringApplicationBuilder application ) {
          application.sources( Application )
      }
  </code></pre>
  
  <p>(I am using the same class for autoconfiguration and servlet init, which is why i just pass my <code>Application</code> class in the configure method)</p>
  
  <p>Looking at the source code for <a href="https://github.com/spring-projects/spring-boot/blob/master/spring-boot/src/main/java/org/springframework/boot/context/web/SpringBootServletInitializer.java">SpringBootServletInitializer</a> it looks like the <code>ErrorPageFilter</code> class is being added by just extending that class here.  I have turned off the <code>ErrorMvcAutoConfiguration</code> - 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 <code>ErrorPageFilter</code>.</p>
  
  <p>I then have my error config file:</p>
  
  <pre><code>@Configuration
  class ErrorConfiguration implements EmbeddedServletContainerCustomizer {
  
      @Override public void customize( ConfigurableEmbeddedServletContainer container ) {
          container.addErrorPages(new ErrorPage( HttpStatus.NOT_FOUND, "/errors/404" ))
      }
  </code></pre>
  
  <p>However, if I just visit an invalid URL, and I <code>DispatcherServlet</code> can't find a match then I just get tomcats /404.html - not my view linked to "<code>/errors/404</code>"  (I have this path mapped to a thymeleaf view template, that works fine - if I navigate to /errors/404 it displays ok)</p>
  
  <p>Any ideas why my custom error page is not working?  tracing the logs, I get a line about the <code>ErrorPageFilter</code> being configured and setup ok on application startup, but then no mentions of the filter doing anything when a request comes in.</p>