EmbeddedServletContainerCustomizer在spring boot 2.0中

时间:2018-03-21 12:49:46

标签: java spring-boot

我尝试将我的应用从spring boot 1.5迁移到2.0 问题是我找不到EmbeddedServletContainerCustomizer。 任何想法如何通过?

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

更新 我在org.springframework.boot.autoconfigure.web.servlet包中找到它作为ServletWebServerFactoryCustomizer。

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

但是有一个错误: 无法解决方法

  

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

我还必须将新错误页面的导入从org.springframework.boot.web.servlet更改为org.springframework.boot.web.server

4 个答案:

答案 0 :(得分:3)

我认为您需要ConfigurableServletWebServerFactory,而不是ServletWebServerFactoryCustomizer

您可以在下面找到代码段:

import org.springframework.boot.web.embedded.undertow.UndertowServletWebServerFactory;
import org.springframework.boot.web.server.ErrorPage;
import org.springframework.boot.web.servlet.server.ConfigurableServletWebServerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpStatus;

@Configuration
public class ServerConfig {

    @Bean
    public ConfigurableServletWebServerFactory webServerFactory() {
        UndertowServletWebServerFactory factory = new UndertowServletWebServerFactory();
        factory.addErrorPages(new ErrorPage(HttpStatus.UNAUTHORIZED, "/unauthenticated"));
        return factory;
    }
}

以上代码适用于Undertow。对于tomcat,您需要将UndertowServletWebServerFactory替换为TomcatServletWebServerFactory

答案 1 :(得分:1)

WebServerFactoryCustomizer 的Spring Boot 2替换了 EmbeddedServletContainerCustomizer

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

或者,您可以添加一个视图控制器,例如

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

,然后您的WebServerFactory应该指向/ unauthorized:

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

答案 2 :(得分:0)

您可以在下面找到代码段:

@Bean
public ErrorPageRegistrar errorPageRegistrar(){
    return new MyErrorPageRegistrar();
}

// ...

private static class MyErrorPageRegistrar implements ErrorPageRegistrar {

    @Override
    public void registerErrorPages(ErrorPageRegistry registry) {
        registry.addErrorPages(new ErrorPage(HttpStatus.BAD_REQUEST, "/400"));
    }

}

答案 3 :(得分:0)

您不需要容器定制器来注册错误页面。实现为lambda的简单bean定义可以解决问题:

@Bean
public ErrorPageRegistrar errorPageRegistrar() {
    return registry -> {
        registry.addErrorPages(
            new ErrorPage(HttpStatus.UNAUTHORIZED, "/unauthenticated"),
        );
    };
}