在J2EE項目的開發中,無論是對底層的數據庫操做過程,仍是業務層的處理過程,仍是控制層的處理過程,都不可避免會遇到各類可預知的、不可預知的異常須要處理。每一個過程都單獨處理異常,系統的代碼耦合度高,工做量大且很差統一,維護的工做量也很大。 那麼,能不能將全部類型的異常處理從各處理過程解耦出來,這樣既保證了相關處理過程的功能較單一,也實現了異常信息的統一處理和維護?答案是確定的。下面將介紹使用Spring MVC統一處理異常的解決和實現過程。前端
(一) SimpleMappingExceptionResolver
使用這種方式具備集成簡單、有良好的擴展性、對已有代碼沒有入侵性等優勢,但該方法僅能獲取到異常信息,若在出現異常時,對須要獲取除異常之外的數據的狀況不適用。web
1 @Configuration 2 @EnableWebMvc 3 @ComponentScan(basePackages = {"com.balbala.mvc.web"}) 4 public class WebMVCConfig extends WebMvcConfigurerAdapter{ 5 @Bean 6 public SimpleMappingExceptionResolver simpleMappingExceptionResolver() 7 { 8 SimpleMappingExceptionResolver b = new SimpleMappingExceptionResolver(); 9 Properties mappings = new Properties(); 10 mappings.put("org.springframework.web.servlet.PageNotFound", "page-404"); 11 mappings.put("org.springframework.dao.DataAccessException", "data-access"); 12 mappings.put("org.springframework.transaction.TransactionException", "transaction-Failure"); 13 b.setExceptionMappings(mappings); 14 return b; 15 } 16 }
(二) HandlerExceptionResolver
相比第一種來講,HandlerExceptionResolver能準確顯示定義的異常處理頁面,達到了統一異常處理的目標
1.定義一個類實現HandlerExceptionResolver接口spring
1 public class GlobalHandlerExceptionResolver implements HandlerExceptionResolver { 2 private static final Logger LOG = LoggerFactory.getLogger(GlobalHandlerExceptionResolver.class); 3 /** 4 * 在這裏處理全部得異常信息 5 */ 6 @Override 7 public ModelAndView resolveException(HttpServletRequest req, HttpServletResponse resp, Object o, Exception ex) { 8 ex.printStackTrace(); 9 if (ex instanceof AthenaException) { 10 //AthenaException爲一個自定義異常 11 ex.printStackTrace(); 12 printWrite(ex.toString(), resp); 13 return new ModelAndView(); 14 } 15 //RspMsg爲一個自定義處理異常信息的類 16 //ResponseCode爲一個自定義錯誤碼的接口 17 RspMsg unknownException = null; 18 if (ex instanceof NullPointerException) { 19 unknownException = new RspMsg(ResponseCode.CODE_UNKNOWN, "業務判空異常", null); 20 } else { 21 unknownException = new RspMsg(ResponseCode.CODE_UNKNOWN, ex.getMessage(), null); } 22 printWrite(unknownException.toString(), resp); 23 return new ModelAndView(); 24 } 25 26 /** 27 * 將錯誤信息添加到response中 28 * 29 * @param msg 30 * @param response 31 * @throws IOException 32 */ 33 public static void printWrite(String msg, HttpServletResponse response) { 34 try { 35 PrintWriter pw = response.getWriter(); 36 pw.write(msg); 37 pw.flush(); 38 pw.close(); 39 } catch (Exception e) { 40 e.printStackTrace(); 41 } 42 } 43 }
這種方式實現的異常處理,能夠針對不一樣的異常和本身定義的異常碼進行翻譯,而後輸出到response中,在前端展現。數據庫
(三)@ExceptionHandlermvc
1.首先定義一個父類,實現一些基礎的方法app
1 public class BaseGlobalExceptionHandler { 2 protected static final Logger logger = null; 3 protected static final String DEFAULT_ERROR_MESSAGE = "系統忙,請稍後再試"; 4 5 protected ModelAndView handleError(HttpServletRequest req, HttpServletResponse rsp, Exception e, String viewName, HttpStatus status) throws Exception { 6 if (AnnotationUtils.findAnnotation(e.getClass(), ResponseStatus.class) != null) 7 throw e; 8 String errorMsg = e instanceof MessageException ? e.getMessage() : DEFAULT_ERROR_MESSAGE; 9 String errorStack = Throwables.getStackTraceAsString(e); 10 11 getLogger().error("Request: {} raised {}", req.getRequestURI(), errorStack); 12 if (Ajax.isAjax(req)) { 13 return handleAjaxError(rsp, errorMsg, status); 14 } 15 return handleViewError(req.getRequestURL().toString(), errorStack, errorMsg, viewName); 16 } 17 18 protected ModelAndView handleViewError(String url, String errorStack, String errorMessage, String viewName) { 19 ModelAndView mav = new ModelAndView(); 20 mav.addObject("exception", errorStack); 21 mav.addObject("url", url); 22 mav.addObject("message", errorMessage); 23 mav.addObject("timestamp", new Date()); 24 mav.setViewName(viewName); 25 return mav; 26 } 27 28 protected ModelAndView handleAjaxError(HttpServletResponse rsp, String errorMessage, HttpStatus status) throws IOException { 29 rsp.setCharacterEncoding("UTF-8"); 30 rsp.setStatus(status.value()); 31 PrintWriter writer = rsp.getWriter(); 32 writer.write(errorMessage); 33 writer.flush(); 34 return null; 35 } 36 37 public Logger getLogger() { 38 return LoggerFactory.getLogger(BaseGlobalExceptionHandler.class); 39 } 40 }
2.針對你須要捕捉的異常實現相對應的處理方式ide
1 @ControllerAdvice 2 public class GlobalExceptionHandler extends BaseGlobalExceptionHandler { 3 4 //好比404的異常就會被這個方法捕獲 5 @ExceptionHandler(NoHandlerFoundException.class) 6 @ResponseStatus(HttpStatus.NOT_FOUND) 7 public ModelAndView handle404Error(HttpServletRequest req, HttpServletResponse rsp, Exception e) throws Exception { 8 return handleError(req, rsp, e, "error-front", HttpStatus.NOT_FOUND); 9 } 10 11 //500的異常會被這個方法捕獲 12 @ExceptionHandler(Exception.class) 13 @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) 14 public ModelAndView handleError(HttpServletRequest req, HttpServletResponse rsp, Exception e) throws Exception { 15 return handleError(req, rsp, e, "error-front", HttpStatus.INTERNAL_SERVER_ERROR); 16 } 17 18 //TODO 你也能夠再寫一個方法來捕獲你的自定義異常 19 //TRY NOW!!! 20 21 @Override 22 public Logger getLogger() { 23 return LoggerFactory.getLogger(GlobalExceptionHandler.class); 24 } 25 26 }