本文主要介紹springmvc中異常處理的思路,并展示如何自定義異常處理類以及全局異常處理器的配置
異常處理思路
系統(tǒng)中異常包括兩類:
- 預(yù)期異常
- 運(yùn)行時異常RuntimeException
前者通過捕獲異常從而獲取異常信息门粪,后者主要通過規(guī)范代碼開發(fā)箩祥、測試通過手段減少運(yùn)行時異常的發(fā)生巷屿。
系統(tǒng)的dao、service治笨、controller出現(xiàn)都通過throws Exception向上拋出夕土,最后由springmvc前端控制器交由異常處理器進(jìn)行異常處理,如下圖:
springmvc提供全局異常處理器(一個系統(tǒng)只有一個異常處理器)進(jìn)行統(tǒng)一異常處理井誉。
自定義異常類
對不同的異常類型定義異常類,繼承Exception整胃。
package com.iot.learnssm.firstssm.exception;
/**
* Created by brian on 2016/3/7.
*
* 系統(tǒng) 自定義異常類颗圣,針對預(yù)期的異常,需要在程序中拋出此類的異常
*/
public class CustomException extends Exception{
//異常信息
public String message;
public CustomException(String message){
super(message);
this.message = message;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
全局異常處理器
思路:
系統(tǒng)遇到異常屁使,在程序中手動拋出在岂,dao拋給service、service給controller蛮寂、controller拋給前端控制器蔽午,前端控制器調(diào)用全局異常處理器。
全局異常處理器處理思路:
解析出異常類型
- 如果該異常類型是系統(tǒng)自定義的異常酬蹋,直接取出異常信息及老,在錯誤頁面展示
- 如果該異常類型不是系統(tǒng)自定義的異常抽莱,構(gòu)造一個自定義的異常類型(信息為“未知錯誤”)
springmvc提供一個HandlerExceptionResolver接口
public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {
//handler就是處理器適配器要執(zhí)行Handler對象(只有method)
//解析出異常類型
//如果該 異常類型是系統(tǒng) 自定義的異常,直接取出異常信息骄恶,在錯誤頁面展示
//String message = null;
//if(ex instanceof CustomException){
//message = ((CustomException)ex).getMessage();
//}else{
////如果該 異常類型不是系統(tǒng) 自定義的異常食铐,構(gòu)造一個自定義的異常類型(信息為“未知錯誤”)
//message="未知錯誤";
//}
//上邊代碼變?yōu)? CustomException customException;
if(ex instanceof CustomException){
customException = (CustomException)ex;
}else{
customException = new CustomException("未知錯誤");
}
//錯誤信息
String message = customException.getMessage();
ModelAndView modelAndView = new ModelAndView();
//將錯誤信息傳到頁面
modelAndView.addObject("message", message);
//指向錯誤頁面
modelAndView.setViewName("error");
return modelAndView;
}
}
錯誤頁面
<%--
Created by IntelliJ IDEA.
User: Brian
Date: 2016/3/4
Time: 10:51
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>錯誤提示</title>
</head>
<body>
${message}
</body>
</html>
在springmvc.xml配置全局異常處理器
<!-- 全局異常處理器
只要實現(xiàn)HandlerExceptionResolver接口就是全局異常處理器
-->
<bean class="com.iot.learnssm.firstssm.exception.CustomExceptionResolver"></bean>
全局異常處理器只有一個,配置多個也沒用僧鲁。
異常測試
在controller虐呻、service、dao中任意一處需要手動拋出異常寞秃。如果是程序中手動拋出的異常斟叼,在錯誤頁面中顯示自定義的異常信息,如果不是手動拋出異常說明是一個運(yùn)行時異常春寿,在錯誤頁面只顯示“未知錯誤”朗涩。
在商品修改的controller方法中拋出異常
public String editItems(Model model,@RequestParam(value="id",required=true) Integer items_id)throws Exception {
//調(diào)用service根據(jù)商品id查詢商品信息
ItemsCustom itemsCustom = itemsService.findItemsById(items_id);
//判斷商品是否為空,根據(jù)id沒有查詢到商品堂淡,拋出異常馋缅,提示用戶商品信息不存在
if(itemsCustom == null){
throw new CustomException("修改的商品信息不存在!");
}
//通過形參中的model將model數(shù)據(jù)傳到頁面
//相當(dāng)于modelAndView.addObject方法
model.addAttribute("items", itemsCustom);
return "items/editItems";
}
在service接口中拋出異常:
public ItemsCustom findItemsById(Integer id) throws Exception {
Items items = itemsMapper.selectByPrimaryKey(id);
if(items==null){
throw new CustomException("修改的商品信息不存在!");
}
//中間對商品信息進(jìn)行業(yè)務(wù)處理
//....
//返回ItemsCustom
ItemsCustom itemsCustom = null;
//將items的屬性值拷貝到itemsCustom
if(items!=null){
itemsCustom = new ItemsCustom();
BeanUtils.copyProperties(items, itemsCustom);
}
return itemsCustom;
}
- 如果與業(yè)務(wù)功能相關(guān)的異常,建議在service中拋出異常绢淀。
- 與業(yè)務(wù)功能沒有關(guān)系的異常萤悴,建議在controller中拋出。