2

Whether the below changes will work fine in spring 5 ?

Please suggest the right way to proceed

Spring 2:

  @Bean
  public EmbeddedServletContainerCustomizer containerCustomizer()
  {
    return container -> 
    {
      container.addErrorPages(new ErrorPage(HttpStatus.NOT_FOUND, "/not-found"));
    };
  }

spring 5 :

@Bean
  public WebServerFactoryCustomizer containerCustomizer()
  {
    return container -> 
    {
      TomcatServletWebServerFactory factory = new TomcatServletWebServerFactory();
    factory.addErrorPages(new ErrorPage(HttpStatus.NOT_FOUND, "/not-found"));
      container = factory;
    };

  }

I referenced the below links,

EmbeddedServletContainerCustomizer in spring boot 2.0

Vanitha V
  • 123
  • 2
  • 13

1 Answers1

2

The equivalent code in Spring Boot 2 is the following:

@Bean
public WebServerFactoryCustomizer<ConfigurableServletWebServerFactory> webServerFactoryCustomizer() {
    return (factory) -> factory.addErrorPages(new ErrorPage(HttpStatus.NOT_FOUND, "/not-found"));
}
Andy Wilkinson
  • 108,729
  • 24
  • 257
  • 242
  • Thanks for your brilliant reply but one thing that I don't get is what is wrong with directly mapping to the error page that we already have? @Bean public WebServerFactoryCustomizer webServerFactoryCustomizer() { return (factory) -> factory.addErrorPages(new ErrorPage(HttpStatus.NOT_FOUND, "/404.html")); } – Shilan Feb 27 '20 at 09:13
  • 1
    There's nothing wrong with that. My example above is specific to the question where `/not-found` was being used. – Andy Wilkinson Feb 27 '20 at 10:27
  • Does anyone know if the Spring Boot 2.x approach work for non-embedded Tomcat? I'm working on an app with 2 WARs in a single TC instance. All of the Spring Boot 1.5.x customization interfaces work only with an embedded Tomcat. – Charlie Reitzel Aug 25 '20 at 14:53
  • Spring Boot 2.x's customization also only applies to embedded Tomcat. If you're deploying a war file to an external Tomcat, you should configure it using server.xml and context.xml. – Andy Wilkinson Aug 25 '20 at 15:11