小编典典

如何在Spring Boot中禁用ErrorPageFilter?

spring

我正在创建应该在Tomcat上运行的SOAP服务。
我正在为应用程序使用Spring Boot,类似于:

@Configuration
@EnableAutoConfiguration(exclude = ErrorMvcAutoConfiguration.class)
public class AppConfig {
}

我的网络服务(示例):

@Component
@WebService
public class MyWebservice {

    @WebMethod
    @WebResult
    public String test() {
        throw new MyException();
    }
}

@WebFault
public class MyException extends Exception {
}

问题:
每当我在webservice类中引发异常时,服务器上都会记录以下消息:

ErrorPageFilter: Cannot forward to error page for request [/services/MyWebservice] 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

问题:
如何防止这种情况?


阅读 1005

收藏
2020-04-20

共1个答案

小编典典

要禁用ErrorPageFilterSpring Boot(已通过1.3.0.RELEASE测试),请在Spring配置中添加以下bean:

@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;
}
2020-04-20