EmbeddedServletContainerCustomizer in spring boot 2.0

degath :

I try to migrate my app from spring boot 1.5 to 2.0 The problem is that I cannot find EmbeddedServletContainerCustomizer. Any ideas how to make it through?

@Bean
public EmbeddedServletContainerCustomizer customizer() {
    return container -> container.addErrorPages(new ErrorPage(HttpStatus.UNAUTHORIZED, "/unauthenticated"));
}

Update: I found it as ServletWebServerFactoryCustomizer in org.springframework.boot.autoconfigure.web.servlet package.

@Bean
public ServletWebServerFactoryCustomizer customizer() {
    return container -> container.addErrorPages(new ErrorPage(HttpStatus.UNAUTHORIZED, "/unauthenticated"));
}

But there is an error: Cannot resolve method

'addErrorPages(org.springframework.boot.web.server.ErrorPage)'

I also had to change import of new Error Page from org.springframework.boot.web.servlet to org.springframework.boot.web.server

Shilan :

From Spring Boot 2 on the WebServerFactoryCustomizer has replaced the EmbeddedServletContainerCustomizer:

@Bean
public WebServerFactoryCustomizer<ConfigurableServletWebServerFactory> webServerFactoryCustomizer() {
    return (factory) -> factory.addErrorPages(new ErrorPage(HttpStatus.UNAUTHORIZED, "/401.html"));
}

Alternatively you might add a view controller like

@Override
public void addViewControllers(ViewControllerRegistry registry) {
    registry.addViewController("/unauthorized").setViewName("forward:/401.html");
}

and then your WebServerFactory should point to /unauthorized instead:

@Bean
public WebServerFactoryCustomizer<ConfigurableServletWebServerFactory> webServerFactoryCustomizer() {
    return (factory) -> factory.addErrorPages(new ErrorPage(HttpStatus.UNAUTHORIZED, "/unauthorized"));
}

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=443813&siteId=1