我正在使用以下泛型類來處理我的應用程序中的所有類型的異常。它處理大多數異常,但對於某些例如「org.apache.tiles.impl.CannotRenderException」失敗。我怎樣才能使它捕獲所有類型的異常?Spring MVC通用異常處理
一些,我使用的技術是:春季4.0.0.RELEASE,瓷磚2.2,Maven的1.6,春天的Webflow 2.4.0.RELEAS
@ControllerAdvice
class GenericDefaultExceptionHandler {
public static final String DEFAULT_ERROR_VIEW = "error/error";
private static final String DEFAULT_ERROR_SUBJECT = "Exception occurred";
final Logger logger = LoggerFactory.getLogger(getClass());
@Autowired
private MailService mailService;
@ExceptionHandler(value = Exception.class)
public ModelAndView defaultErrorHandler(HttpServletRequest req, Exception e) throws Exception {
if (AnnotationUtils.findAnnotation(e.getClass(), ResponseStatus.class) != null)
throw e;
ModelAndView mav = new ModelAndView();
mav.addObject("exception", e);
mav.addObject("url", req.getRequestURL());
mav.setViewName(DEFAULT_ERROR_VIEW);
//send email to system admin
sendMessageToAdmin(e.toString(),req.getRequestURL().toString());
logger.error(e.toString());
return mav;
}
private void sendMessageToAdmin(String exceptionAsMessage, String url) {
try {
StringBuilder errorMessage = new StringBuilder();
errorMessage.append("Exception on request URL :");
errorMessage.append(url);
errorMessage.append("\n\n");
errorMessage.append("The Exception was: ");
errorMessage.append(exceptionAsMessage);
mailService.sendMailWithSubject(DEFAULT_ERROR_SUBJECT,errorMessage.toString());
} catch (Exception e) {
}
}
}
感謝
過濾器是確定的方式。我們的異常過濾器通過電子郵件發送我們網站中發生的所有異常,並向電子郵件添加有趣的元數據:會話ID,遠程地址,用戶代理等......但是我們也列出了一些異常,例如套接字異常,每天的時間,而且毫無意義。 – hooknc 2014-09-29 21:37:11