How to disable ErrorPageFilter in Spring Boot?

70,135

Solution 1

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

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

Solution 2

The simpliest way to disable ErrorPageFilter is:

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

Solution 3

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

}

Solution 4

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.

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

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

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

I prefer the first way.Hope this helps.

Solution 5

I found in the sources that the ErrorPageFilter.java has the following code:

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.

The way to remove the ErrorPageFilter is like this

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

Chris

Share:
70,135

Related videos on Youtube

membersound
Author by

membersound

JEE + Frameworks like Spring, Hibernate, JSF, GWT, Vaadin, SOAP, REST.

Updated on July 24, 2022

Comments

  • membersound
    membersound almost 2 years

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

    @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:

    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


    Question:
    How can I prevent this?

    • Andy Wilkinson
      Andy Wilkinson about 9 years
      Can you share some code that reproduces the problem?
  • Dan
    Dan over 7 years
    Were you using an embedded container for this? Or a war deployment?
  • Calabacin
    Calabacin over 7 years
    This seems to be the official way to do it, according to the bug report in GitHub. This is the fix they added: github.com/spring-projects/spring-boot/commit/…
  • Linuslabo
    Linuslabo over 7 years
    Please give a look at: How to write a good answer
  • Sõber
    Sõber about 7 years
    This solution does not work since Spring Boot 1.5.0.
  • Trynkiewicz Mariusz
    Trynkiewicz Mariusz about 7 years
    @Sõber it's possibly a bug, as you can see nothing has changed here github.com/spring-projects/spring-boot/blame/…
  • cody.tv.weber
    cody.tv.weber over 4 years
    Embedded tomcat has ErrorPageFilter stripped out from my understanding...