스프링 MVC에서 SimpleMappingExceptionResolver 선언 시 주의 사항

프로그래밍|2013. 4. 18. 15:35

스프링 MVC에서 SimpleMappingExceptionResolver를 서블릿 컨텍스트 설정 파일에 선언하게 되면 예외 발생 시 익셉션이 로그에 남지 않는 현상이 발생했다. 즉, 리졸버에 선언되어 있는 에러 페이지만 브라우저에 노출하고 에러 로그는 어디에도 남지 않는 현상이다.

<bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
	<property name="order" value="1" />
	<property name="defaultErrorView" value="common/error/defaultError" />
	<property name="exceptionMappings">
		<props>
			<prop key="BusinessLogicException">common/error/businessLogicError</prop>
			<prop key="RuntimeException">common/error/runtimeError</prop>
			<prop key="TypeMismatchException">common/error/defaultError</prop>
		</props>
	</property>
</bean>


DispatcherServlet 소스를 확인해 보았다.

catch (Exception ex) {
	Object handler = (mappedHandler != null ? mappedHandler.getHandler() : null);
	mv = processHandlerException(processedRequest, response, handler, ex);
	errorView = (mv != null);
}


예외 발생 시 processHandlerException 메소드 안에서는 등록되어 있는 익셉션 리졸버를 찾은 후 ModelAndView를 return만 하고, 익셉션 내용은 찍지 않는다. ㅠㅠ


결국 SimpleMappingExceptionResolver를 상속하는 커스텀 클래스를 생성하여 에러 로그를 남길 수 있도록 처리하였다.

private static Logger logger = LoggerFactory.getLogger(CustomSimpleMappingExceptionResolver.class);

@Override
public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {
	logger.error("##ERROR", ex);
	return super.resolveException(request, response, handler, ex);
}


댓글()