본문 바로가기
일이야기/Spring

Tomcat 구동시 ErrorPageFilter 에러 처리

by Cloud9™ 2019. 9. 18.

기본적으로 Spring boot에서는 에러를 처리하는 filter가 자동으로 동작한다.

하지만 프로젝트에 따라 Application내에서 에러 핸들링을 달리해야 하는데

Filter에서 선처리하고 아래와 같은 에러 로그를 출력한다.

 

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

 

 

새로운 Bean을 생성해서 FilterRegistrationBean에 등록하고 Enabled를 false로 설정해서

filter가 동작하지 않도록 아래와 같이 처리한다.

 

@Bean      

public ErrorPageFilter errorPageFilter() {     

      return new ErrorPageFilter();     

}   


@Bean  

public FilterRegistrationBean DisabledErrorPageFilter(ErrorPageFilter filter) {  

      FilterRegistrationBean filterRegistration = new FilterRegistrationBean<>();  

      filterRegistration.setFilter(filter);  

      filterRegistration.setName("disabledErrorPageFilter");  

      filterRegistration.setEnabled(false);  

      return filterRegistration;  

}

 

그런데 여기서 한가지 문제가...

 

Spring boot 2.1 부터는 Bean Overriding이 불가능해서 위와 같이 바로 적용하면 아래와 같은 에러가 발생한다.

 

The bean 'ErrorPageFilter', defined in class path resource [ErrorPageRegistry.class], could not be registered. A bean with that name has already been defined in class path resource [ErrorPageFilter.class] and overriding is disabled.

이럴 땐 aplication.yml 파일에 등록하고 사용하면 된다.

 

spring:
   main: 
     allow-bean-definition-overriding: true

 

 

더 간단한 방법도 있네요.(2020.07.01 추가)

 

@SpringBootApplication public class App extends SpringBootServletInitializer {
    public App() {
        super();
        setRegisterErrorPageFilter(false); // <- this one
    }
   ..

'일이야기 > Spring' 카테고리의 다른 글

생성자 주입에 대하여...  (0) 2022.05.17
mybatis에서 NumberformatException 처리  (0) 2020.07.01
BasicErrorController.error Ambiguous 처리  (0) 2019.09.18

댓글