Java 如何在 Spring Boot 中禁用 ErrorPageFilter?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30170586/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 09:15:57  来源:igfitidea点击:

How to disable ErrorPageFilter in Spring Boot?

javaspringweb-servicessoapspring-boot

提问by membersound

I'm creating a SOAPservice that should be running on Tomcat.
I'm using Spring Boot for my application, similar to:

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

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


My webservice (example):


我的网络服务(示例):

@Component
@WebService
public class MyWebservice {

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

@WebFault
public class MyException extends Exception {
}


Problem:
Whenever I throw an exception within the webservice class, the following message is logged on the server:


问题:
每当我在 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

ErrorPageFilter:无法转发到请求 [/services/MyWebservice] 的错误页面,因为响应已经提交。因此,响应可能具有错误的状态代码。如果您的应用程序在 WebSphere Application Server 上运行,您可以通过将 com.ibm.ws.webcontainer.invokeFlushAfterService 设置为 false 来解决此问题


Question:
How can I prevent this?


问题:
我怎样才能防止这种情况?

采纳答案by mzc

To disable the ErrorPageFilterin Spring Boot (tested with 1.3.0.RELEASE), add the following beans to your Spring configuration:

要禁用ErrorPageFilterSpring Boot(使用 1.3.0.RELEASE 测试),请将以下 bean 添加到 Spring 配置中:

@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;
}

回答by Christopher Rivera

I found in the sources that the ErrorPageFilter.javahas the following code:

我在来源中发现ErrorPageFilter.java具有以下代码:

private void doFilter(HttpServletRequest request, HttpServletResponse response,
        FilterChain chain) throws IOException, ServletException {
    ErrorWrapperResponse wrapped = new ErrorWrapperResponse(response);
    try {
        chain.doFilter(request, wrapped);
        int status = wrapped.getStatus();
        if (status >= 400) {
            handleErrorStatus(request, response, status, wrapped.getMessage());
            response.flushBuffer();
        }
        else if (!request.isAsyncStarted() && !response.isCommitted()) {
            response.flushBuffer();
        }
    }
    catch (Throwable ex) {
        handleException(request, response, wrapped, ex);
        response.flushBuffer();
    }
}

As you can see when you throw an exception and return a response code >= 400 it will do some code. there should be some additional check if the response was already committed or not.

正如您所看到的,当您抛出异常并返回响应代码 >= 400 时,它会执行一些代码。应该有一些额外的检查响应是否已经提交。

The way to remove the ErrorPageFilter is like this

去掉ErrorPageFilter的方法是这样的

protected WebApplicationContext run(SpringApplication application) {
    application.getSources().remove(ErrorPageFilter.class);
    return super.run(application);
}

Chris

克里斯

回答by Trynkiewicz Mariusz

The simpliest way to disable ErrorPageFilter is:

禁用 ErrorPageFilter 的最简单方法是:

@SpringBootApplication
public class App extends SpringBootServletInitializer {

public App() {
    super();
    setRegisterErrorPageFilter(false); // <- this one
}

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

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

回答by jimlgx

    @SpringBootApplication
public class MyApplication extends SpringBootServletInitializer {
    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        //set register error pagefilter false
        setRegisterErrorPageFilter(false);

        builder.sources(MyApplication.class);
        return builder;
    }

}

回答by manish negi

public class Application extends SpringBootServletInitializer 
{
   private static final Logger logger = LogManager.getLogger(Application.class);

   public Application()
   {
       super();
       setRegisterErrorPageFilter(false);
   }

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

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

回答by Madhu Stv

The best way is to tell the WebSphere container to stop ErrorPageFiltering. To achieve this we have to define a property in the server.xml file.

最好的方法是告诉 WebSphere 容器停止 ErrorPageFiltering。为此,我们必须在 server.xml 文件中定义一个属性。

<webContainer throwExceptionWhenUnableToCompleteOrDispatch="false" invokeFlushAfterService="false"></webContainer>

<webContainer throwExceptionWhenUnableToCompleteOrDispatch="false" invokeFlushAfterService="false"></webContainer>

Alternatively, you also can disable it in the spring application.properties file

或者,您也可以在 spring application.properties 文件中禁用它

logging.level.org.springframework.boot.context.web.ErrorPageFilter=off

I prefer the first way.Hope this helps.

我更喜欢第一种方式。希望这会有所帮助。