SimpleMappingExceptionResolver is generally used to redirect to an error page. It can be extended to log the trace to a log file.
public class LoggingExceptionResolver extends SimpleMappingExceptionResolver {
private static Log log = LogFactory.getLog(LoggingExceptionResolver.class);
public LoggingExceptionResolver() {
super();
}
@Override
public ModelAndView resolveException(HttpServletRequest request,
HttpServletResponse response, Object handler, Exception ex) {
log.debug(getStackTrace(ex));
return super.resolveException(request, response, handler, ex);
}
/**
* @param t , Throwable
* @return String of the exception
*/
public String getStackTrace(Throwable t) {
StringWriter stringWritter = new StringWriter();
PrintWriter printWritter = new PrintWriter(stringWritter, true);
t.printStackTrace(printWritter);
printWritter.flush();
stringWritter.flush();
return stringWritter.toString();
}
}
Configure this bean,
< bean id="exceptionResolver" class="com.common.service.LoggingExceptionResolver" >
</bean >
public class LoggingExceptionResolver extends SimpleMappingExceptionResolver {
private static Log log = LogFactory.getLog(LoggingExceptionResolver.class);
public LoggingExceptionResolver() {
super();
}
@Override
public ModelAndView resolveException(HttpServletRequest request,
HttpServletResponse response, Object handler, Exception ex) {
log.debug(getStackTrace(ex));
return super.resolveException(request, response, handler, ex);
}
/**
* @param t , Throwable
* @return String of the exception
*/
public String getStackTrace(Throwable t) {
StringWriter stringWritter = new StringWriter();
PrintWriter printWritter = new PrintWriter(stringWritter, true);
t.printStackTrace(printWritter);
printWritter.flush();
stringWritter.flush();
return stringWritter.toString();
}
}
Configure this bean,