tomcat java.lang.IllegalArgumentException:筛选器不能为空

h4cxqtbf  于 12个月前  发布在  Java
关注(0)|答案(1)|浏览(173)

随着Springboot从2.7.2更新到3.2.0,现在Filter类开始抛出异常
原因:org. springframework. Boot . web. server. WebServerException:无法在org. springframework. Boot . web. embedded. tomcat. TomcatWebServer. initialize启动嵌入式Tomcat(TomcatWebServer.java:142)位于org.springframework.boot.web.embedded.tomcat.TomcatWebServer。(TomcatWebServer.java:104)at org. springframework. Boot . web. embedded. tomcat. TomcatServletWebServerFactory. getTomcatWebServer(TomcatServletWebServerFactory.java:501)at org. springframework. Boot . web. embedded. tomcat. TomcatServletWebServerFactory. getWebServer(TomcatServletWebServerFactory.java:218)at org. springframework. Boot . web. servlet. context. ServletWebServerApplicationContext. WebServer(ServletWebServerApplicationContext.java:188)at org. springframework. Boot . web. servlet. context. ServletWebServerApplicationContext. onRefresh(ServletWebServerApplicationContext.java:162). 11 common frames omitted by:java. lang. IllegalArgumentException:筛选器在org. springframework. util. Assert. notbox中不能为null(Assert.java:172)at org. springframework. Boot . web. servlet. AbstractFilterRegistrationBean. getDescription(AbstractFilterRegistrationBean.java:223)at org. springframework. Boot . web. servlet. RegistrationBean. onStartup(RegistrationBean.java:47)at org. springframework. Boot . web. servlet. context. ServletWebServerApplicationContext. selfInitialize(ServletWebServerApplicationContext.java:241)at org. springframework. Boot . web. embedded. tomcat. TomcatStarter. onStartup(TomcatStarter.java:52)位于org. apache. Catalina . core. StandardContext. startInternal(StandardContext.java:4850)at org. apache. Catalina . util. LifecleBase. start(LifecleBase.java:171)at org. apache. Catalina . core. ContainerBase $www.example.com(ContainerBase.java:1332)at org. apache. Catalina . core. ContainerBase $www.example.com(ContainerBase.java:1322)at java. base/java. util. concurrent. FutureTask. run(FutureTask.java:在org. apache. tomcat. util. threads. InlineExecutorService. execute(InlineExecutorService.java:75)在java. base/java. util. concurrent. AbstractExecutorService. submit(AbstractExecutorService.java:StartChild.call866)at org. apache. Catalina . core. StandardEngine. startInternal(StandardEngine.java:240)在org. apache. Catalina . util. LifecleBase. start(Lifecycle Base.java:地址:org. apache. Catalina . core. StandardService. startInternal(StandardService.java:433)在org. apache. Catalina . util. LifecleBase. start(LifecycleBase.java:171)在org. apache. Catalina . core. StandardServer. startInternal(StandardServer.java:在org. apache. Catalina . util. LifecleBase. start(LifecycleBase.java:171)at org. apache. Catalina . startup. Tomcat. start(Tomcat.java:488)at org. springframework. Boot . web. embedded. tomcat. TomcatWebServer. initialize(TomcatWebServer.java:123).
看起来像抛出异常的代码

@Configuration
public class WebMvcConfig implements  WebMvcConfigurer  {

    @Value("${system.debug.enabled:false}")
    private boolean debugEnabled;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(httpHeaderInterceptor());
    }

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/log/**").allowedOrigins("*")
                .allowedMethods("POST", "GET", "PUT", "PATCH", "OPTIONS", "DELETE")
                .allowedHeaders("header1", "header2", "header3")
                .allowedHeaders("Origin", "Authorization", "X-Requested-With", "Content-Type", "Accept")
                .allowCredentials(false).maxAge(3600);
    }

    @Bean
    public HandlerInterceptor httpHeaderInterceptor() {
        return (HandlerInterceptor) new HttpHeaderInterceptor();
    }
     
    private FilterRegistrationBean newFilterRegistrationBean(Filter filter, int order) {
        final FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
        filterRegistrationBean.setOrder(order);
        filterRegistrationBean.addUrlPatterns("/*");
        return filterRegistrationBean;
    }

    @Bean
    public FilterRegistrationBean stopWatchFilter() {
        Filter filter = debugEnabled ? new StopWatchFilter() : new NoopFilter();
        FilterRegistrationBean filterRegistrationBean = newFilterRegistrationBean(filter, 1);
        return filterRegistrationBean;
    }

    @Bean
    public FilterRegistrationBean gzipFilter() {
        GZipRequestFilter gzipRequestFilter = new GZipRequestFilter();
        return newFilterRegistrationBean(gzipRequestFilter, 2);
    }

    @Bean
    public FilterRegistrationBean registerOpenSessionInViewFilterBean() {
        OpenEntityManagerInViewFilter openEntityManagerInViewFilter = new OpenEntityManagerInViewFilter();
        return newFilterRegistrationBean(openEntityManagerInViewFilter, 1);
    }
}

字符串
根据我的理解,

@Bean
    public FilterRegistrationBean gzipFilter() {
        GZipRequestFilter gzipRequestFilter = new GZipRequestFilter();
        return newFilterRegistrationBean(gzipRequestFilter, 2);
    }

    @Bean
    public FilterRegistrationBean registerOpenSessionInViewFilterBean() {
        OpenEntityManagerInViewFilter openEntityManagerInViewFilter = new OpenEntityManagerInViewFilter();
        return newFilterRegistrationBean(openEntityManagerInViewFilter, 1);
    }


但是place类正在初始化,但仍然告诉NULL。删除此代码后,它将启动服务器。

7ivaypg9

7ivaypg91#

您需要将filter参数传递给FilterRegistrationBean构造函数,如下所示

private FilterRegistrationBean newFilterRegistrationBean(Filter filter, int order) {
        final FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(filter);
        filterRegistrationBean.setOrder(order);
        filterRegistrationBean.addUrlPatterns("/*");
        return filterRegistrationBean;
    }

字符串

相关问题