Spring Boot 2.0 中的 EmbeddedServletContainerCustomizer [英] EmbeddedServletContainerCustomizer in spring boot 2.0
问题描述
我尝试将我的应用从 spring boot 1.5 迁移到 2.0问题是我找不到 EmbeddedServletContainerCustomizer.任何想法如何通过?
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"));
}
更新:我在 org.springframework.boot.autoconfigure.web.servlet 包中发现它是 ServletWebServerFactoryCustomizer.
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)'
'addErrorPages(org.springframework.boot.web.server.ErrorPage)'
我还必须将新错误页面的导入从 org.springframework.boot.web.servlet
更改为 org.springframework.boot.web.server
I also had to change import of new Error Page from org.springframework.boot.web.servlet
to org.springframework.boot.web.server
推荐答案
从 Spring Boot 2 上的 WebServerFactoryCustomizer 已经取代了 EmbeddedServletContainerCustomizer:
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"));
}
或者你可以添加一个像
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/unauthorized").setViewName("forward:/401.html");
}
然后你的 WebServerFactory 应该指向/unauthorized :
and then your WebServerFactory should point to /unauthorized instead:
@Bean
public WebServerFactoryCustomizer<ConfigurableServletWebServerFactory> webServerFactoryCustomizer() {
return (factory) -> factory.addErrorPages(new ErrorPage(HttpStatus.UNAUTHORIZED, "/unauthorized"));
}
这篇关于Spring Boot 2.0 中的 EmbeddedServletContainerCustomizer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!