spring-bootspring-securityembeddedwebserver

EmbeddedServletContainerCustomizer(spring 2 and spring boot 2) changed to WebServerFactoryCustomizer (spring 5 and spring boot 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


Solution

  • 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"));
    }