springboot项目出现ErrorPageFilter异常--处理方案

springboot项目出现ErrorPageFilter异常--处理方案

今天用springboot(2.2.12.RELEASE)+beetl模板的时候,由于某个模板找不到,系统一直出现报错日子:

[http-nio-8080-exec-1] ERROR o.s.b.w.s.s.ErrorPageFilter - [handleCommittedResponse,219] - Cannot forward to error page for request [/yuantuannews/list/index_1.html] as the response has already been committed. As a result, the response may have the wrong status code. If your application is running on WebSphere Application Server you may be able to resolve this problem by setting com.ibm.ws.webcontainer.invokeFlushAfterService to false

看了网上的一些解决方法,大体上都是重写ErrorPageFilter,然后在FilterRegistrationBean中设置 filterRegistrationBean.setEnabled(false);
代码如下:

    @Bean
    public ErrorPageFilter errorPageFilter() {
        return new ErrorPageFilter();
    }
 
    @Bean
    public FilterRegistrationBean disableSpringBootErrorFilter(ErrorPageFilter filter) {
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
        filterRegistrationBean.setFilter(filter);
        filterRegistrationBean.setEnabled(false);
        return filterRegistrationBean;
    }

按照这个方法,我做了多次尝试,系统直接报错说errorPageFilter冲突了,原来是
ErrorPageFilterConfiguration.java中已经定义了这么一个bean:

//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package org.springframework.boot.web.servlet.support;

import javax.servlet.DispatcherType;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration(
    proxyBeanMethods = false
)
class ErrorPageFilterConfiguration {
    ErrorPageFilterConfiguration() {
    }

    @Bean
    ErrorPageFilter errorPageFilter() {
        return new ErrorPageFilter();
    }

    @Bean
    FilterRegistrationBean<ErrorPageFilter> errorPageFilterRegistration(ErrorPageFilter filter) {
        FilterRegistrationBean<ErrorPageFilter> registration = new FilterRegistrationBean(filter, new ServletRegistrationBean[0]);
        registration.setOrder(filter.getOrder());
        registration.setDispatcherTypes(DispatcherType.REQUEST, new DispatcherType[]{DispatcherType.ASYNC});
        return registration;
    }
}

最后我的解决方式是在启动类中设置:

@SpringBootApplication
public class App extends SpringBootServletInitializer {

public App() {
    super();
    //下面设置为false
    setRegisterErrorPageFilter(false); 
}

@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
    return application.sources(App.class);
}

public static void main(String[] args) {
    SpringApplication.run(App.class, args);
}

问题解决。

你可能感兴趣的:(常见问题,ErrorPageFilter)