全局异常处理_全局异常处理
生活随笔
收集整理的這篇文章主要介紹了
全局异常处理_全局异常处理
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
異常處理思路
系統中異常包括兩類:預期異常和運行時異常RuntimeException,前者通過捕獲異常從而獲取異常信息,后者主要通過規范代碼開發、測試通過手段減少運行時異常的發生。系統的dao、service、controller出現都通過throws Exception向上拋出,最后由springmvc前端控制器交由異常處理器進行異常處理,springmvc提供全局異常處理器進行統一的異常處理,一個系統只有一個一個異常處理器。
定義全局異常處理器
1、定義自定義異常類,繼承Exception
public class CustomException extends Exception { public String message; public Throwable throwable; public CustomException(String message){ super(message); this.message = message; } public CustomException(Throwable throwable){ super(throwable); this.throwable = throwable; } public Throwable getThrowable() { return throwable; } public void setThrowable(Throwable throwable) { this.throwable = throwable; } public String getMessage() { return message; } public void setMessage(String message) { this.message = message; }}2、定義異常處理器
public class CustomExceptionResolver implements HandlerExceptionResolver { public ModelAndView resolveException(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) { // 解析出異常類型 CustomException customException = null; String message = ""; // 若該異常類型是系統自定義的異常,直接取出異常信息在錯誤頁面展示即可。 if(e instanceof CustomException){ customException = (CustomException)e; customException.getThrowable().getClass().getName(); }else{ // 如果不是系統自定義異常,構造一個系統自定義的異常類型,信息為“未知錯誤” customException = new CustomException("未知錯誤"); message = customException.getMessage(); } //錯誤信息 ModelAndView modelAndView = new ModelAndView(); //判斷請求的類型, 是否是json HandlerMethod handlerMethod = (HandlerMethod) o; ResponseBody responseBody = handlerMethod.getMethod().getAnnotation(ResponseBody.class); //是json請求, 則放回json數據 if (responseBody != null){ //設置狀態碼和信息 Map<String, Object> responseMap = new HashMap<String, Object>(); responseMap.put("code", "0"); responseMap.put("message", message); String json = new Gson().toJson(responseMap); System.out.println(json); httpServletResponse.setContentType("UTF-8"); httpServletResponse.setContentType("application/json;charset=utf-8"); try{ httpServletResponse.getWriter().write(json); httpServletResponse.getWriter().flush();; }catch (IOException e1){ e1.printStackTrace(); } return modelAndView; } //將錯誤信息傳到頁面 modelAndView.addObject("message",message); //指向錯誤頁面 modelAndView.setViewName("showError"); return modelAndView; }}3、SpringMVC中配置全局異常處理器
class="com.test.exception.CustomExceptionResolver"/>4、測試
@RequestMapping(value="") @ResponseBody public Map<String,Object> Register(User user) throws Exception{ Map<String,Object> map = new HashMap<String,Object>(); try{ boolean isSuccess = userService.Register(user); if(isSuccess){ map.put("tip", "success"); } else{ map.put("tip", "error"); } }catch (Exception e){ throw new CustomException("未知錯誤"); } return map; }如果與業務功能相關的信息,建議在service中拋出異常
與業務功能沒有關系的信息,建議在Controlle r中拋出異常
前后端未分離的異常是這樣處理的,前后端分離的。便不能這樣處理。下次再說前后端分離 的異常處理。
歡迎關注“Java引導者”,我們分享最有價值的Java干貨文章,助力您成為有思想的Java開發工程師!
總結
以上是生活随笔為你收集整理的全局异常处理_全局异常处理的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 003开头新股第一天交易规则
- 下一篇: 编辑函数求n阶乘fact_n!函数用C语