Spring MVC自定义统一异常处理类,并且在控制台中输出错误日志

 在使用SimpleMappingExceptionResolver实现统一异常处理后(参考Spring MVC的异常统一处理方法),发现出现异常时,log4j无法在控制台输出错误日志。因此需要自定义一个继承至SimpleMappingExceptionResolver的RrtongMappingExceptionResolver类,在RrtongMappingExceptionResolver中通过log.error(ex.getMessage())的方式输出日志到控制台上。以下是具体的配置和RrtongMappingExceptionResolver的实现。

         配置自定义的统一异常处理类RrtongMapping


     
     ../../exception/error-interface
     ../../exception/errorPage500404
     
     
ExceptionResolver

public class RrtongMappingExceptionResolver extends SimpleMappingExceptionResolver{  
    private final static Logger log = LoggerFactory.getLogger(RrtongMappingExceptionResolver.class);   
              
    @Override  
    protected ModelAndView doResolveException(HttpServletRequest request, HttpServletResponse response,  
            Object handler, Exception ex) {  
        Map model = new HashMap();  
        model.put("ex", ex);  
        ModelAndView modelAndView = new ModelAndView("../../exception/errorPage",model);  
          
        /*错误日志输出到控制台*/  
        log.error(ex.getMessage());  
                  
        return modelAndView;  
    }  
}  


你可能感兴趣的:(代码)