spring mvc 에서 exception handling 하기


서블릿 웹 애플리케이션에서는 다음과 같이 web.xml에 에러 페이지를 세팅할 수 있다. 

<error-page>
<error-code>404</error-code>
<location>404error.html</location>
</error-page> 
하지만 이런 에러들이 발생했을때 에러페이지로 이동하기전에 로깅등의 작업을 하고 싶을때는 어떻게 해야할까. 보통 실 운영서버에서 log4j같은 것을 사용하면 수많은 로그레벨(info, error, fatal 등)이 섞여 기록될 것이다. 

필요에 의해 에러를 특정페이지로 이동시킨다거나, 에러페이지 이동전에 무엇인가 작업을 해주고 싶다면, spring mvc에서는 handlerExceptionResolver 인터페이스를 구현해 사용하면된다. 

(참조) http://static.springsource.org/spring/docs/3.1.x/javadoc-api/ 에서 HandlerExceptionResolver를 검색해보면 된다. 

spring mvc에서 exception 처리에 대해 수없이 설명하는 SimpleMappingExceptionResolver도 이 인터페이스를 구현한 것이다. 



** org.springframework.web.servlet 패키지가 spring mvc를 분석하는 entry point 인듯.


댓글

이 블로그의 인기 게시물

Session 대신 JWT를 사용하는 이유

VSCode에서의 VIM 단축키와 키보드 구매 가이드

우분투에서 테스트링크(testlink)와 맨티스(mantis)로 테스팅 서버 구성하기