文章目錄
- 1.異常處理的問題分析
- 2.異常處理流程
- 3.可預知異常處理
- 1.自定義異常類
- 2.異常拋出類
- 3.異常捕獲類
- 4.異常處理測試
- 1)定義錯誤代碼
- 2)異常處理測試
- 4.不可預知異常處理
- 1.定義異常捕獲方法
- 1)異常拋出測試
- 2)異常捕獲方法
1.異常處理的問題分析
從添加頁面的service方法中找問題:
文件位置:com/ xuecheng/ manage_cms/ service/ PageService
//添加頁面
public CmsPageResult add(CmsPage cmsPage){
//校驗頁面是否存在,根據頁面名稱、站點Id、頁面webpath查詢
CmsPage cmsPage1=cmsPageRepository.findByPageNameAndSiteIdAndPageWebPath(cmsPage.getPageName(),cmsPage.getSiteId(),cmsPage.getPageWebPath());
if(cmsPage1==null){
cmsPage.setPageId(null);//添加頁面主鍵由spring data 自動生成
cmsPageRepository.save(cmsPage);
//返回結果
CmsPageResult cmsPageResult = new CmsPageResult(CommonCode.SUCCESS,cmsPage);
return cmsPageResult;
}
return new CmsPageResult(CommonCode.FAIL,null);
}
問題:
1)上邊的代碼只要操作不成功僅向用戶返回“錯誤代碼:11111,失敗信息:操作失敗”,無法區別具體的錯誤信
息。
2)service方法在執行過程出現異常在哪捕獲?在service中需要都加try/catch,如果在controller也需要添加
try/catch,代碼冗余嚴重且不易維護。
解決方案:
1)在Service方法中的編碼順序是先校驗判斷,有問題則拋出具體的異常信息,最后執行具體的業務操作,返回成
功信息。
2)在統一異常處理類中去捕獲異常,無需controller捕獲異常,向用戶返回統一規范的響應信息。
代碼模板如下:
//添加頁面
public CmsPageResult add(CmsPage cmsPage){
//校驗cmsPage是否為空
if(cmsPage == null){
//拋出異常,非法請求
//...
}
//根據頁面名稱查詢(頁面名稱已在mongodb創建了唯一索引)
CmsPage cmsPage1 =cmsPageRepository.findByPageNameAndSiteIdAndPageWebPath(cmsPage.getPageName(),cmsPage.getSiteId(),cmsPage.getPageWebPath());
//校驗頁面是否存在,已存在則拋出異常
if(cmsPage1 !=null){
//拋出異常,已存在相同的頁面名稱
//...
}
cmsPage.setPageId(null);//添加頁面主鍵由spring data 自動生成
CmsPage save = cmsPageRepository.save(cmsPage);
//返回結果
CmsPageResult cmsPageResult = new CmsPageResult(CommonCode.SUCCESS,save);
return cmsPageResult;
}
2.異常處理流程
系統對異常的處理使用統一的異常處理流程:
1、自定義異常類型。
2、自定義錯誤代碼及錯誤信息。
3、對于可預知的異常由程序員在代碼中主動拋出,由SpringMVC統一捕獲。
可預知異常是程序員在代碼中手動拋出本系統定義的特定異常類型,由于是程序員拋出的異常,通常異常信息比較齊全,程序員在拋出時會指定錯誤代碼及錯誤信息,獲取異常信息也比較方便。
4、對于不可預知的異常(運行時異常)由SpringMVC統一捕獲Exception類型的異常。
不可預知異常通常是由于系統出現bug、或一些不要抗拒的錯誤(比如網絡中斷、服務器宕機等),異常類型為RuntimeException類型(運行時異常)。
5、可預知的異常及不可預知的運行時異常最終會采用統一的信息格式(錯誤代碼+錯誤信息)來表示,最終也會隨請求響應給客戶端。
1、在controller、service、dao中程序員拋出自定義異常;springMVC框架拋出框架異常類型
2、統一由異常捕獲類捕獲異常,并進行處理
3、捕獲到自定義異常則直接取出錯誤代碼及錯誤信息,響應給用戶。
4、捕獲到非自定義異常類型首先從Map中找該異常類型是否對應具體的錯誤代碼,如果有則取出錯誤代碼和錯誤信息并響應給用戶,如果從Map中找不到異常類型所對應的錯誤代碼,則統一為99999錯誤代碼并響應給用戶。
5、將錯誤代碼及錯誤信息以Json格式響應給用戶。
3.可預知異常處理
1.自定義異常類
文件位置:com/ xuecheng/ framework/ exception/ CustomException
package com.xuecheng.framework.exception;import com.xuecheng.framework.model.response.ResultCode;/*** 自定義異常類型**/
public class CustomException extends RuntimeException {//錯誤代碼ResultCode resultCode;public CustomException(ResultCode resultCode){this.resultCode = resultCode;}public ResultCode getResultCode(){return resultCode;}}
2.異常拋出類
文件位置:com/ xuecheng/ framework/ exception/ ExceptionCast
package com.xuecheng.framework.exception;
import com.xuecheng.framework.model.response.ResultCode;
public class ExceptionCast {
//使用此靜態方法拋出自定義異常
public static void cast(ResultCode resultCode){
throw new CustomException(resultCode);
}
}
3.異常捕獲類
使用 @ControllerAdvice和@ExceptionHandler注解來捕獲指定類型的異常
文件位置:com/ xuecheng/ framework/ exception/ ExceptionCatch
package com.xuecheng.framework.exception;import com.google.common.collect.ImmutableMap;
import com.xuecheng.framework.model.response.CommonCode;
import com.xuecheng.framework.model.response.ResponseResult;
import com.xuecheng.framework.model.response.ResultCode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;/*** 統一異常捕獲類**/
@ControllerAdvice//控制器增強
public class ExceptionCatch {private static final Logger LOGGER = LoggerFactory.getLogger(ExceptionCatch.class);//捕獲CustomException此類異常@ExceptionHandler(CustomException.class)@ResponseBodypublic ResponseResult customException(CustomException customException){//記錄日志LOGGER.error("catch exception:{}",customException.getMessage());ResultCode resultCode = customException.getResultCode();return new ResponseResult(resultCode);}}
4.異常處理測試
1)定義錯誤代碼
每個業務操作的異常使用異常代碼去標識
文件位置:com/ xuecheng/ framework/ domain/ cms/ response / CmsCode
package com.xuecheng.framework.domain.cms.response;import com.xuecheng.framework.model.response.ResultCode;
import lombok.ToString;@ToString
public enum CmsCode implements ResultCode {CMS_ADDPAGE_EXISTSNAME(false,24001,"頁面名稱已存在!"),CMS_GENERATEHTML_DATAURLISNULL(false,24002,"從頁面信息中找不到獲取數據的url!"),CMS_GENERATEHTML_DATAISNULL(false,24003,"根據頁面的數據url獲取不到數據!"),CMS_GENERATEHTML_TEMPLATEISNULL(false,24004,"頁面模板為空!"),CMS_GENERATEHTML_HTMLISNULL(false,24005,"生成的靜態html為空!"),CMS_GENERATEHTML_SAVEHTMLERROR(false,24005,"保存靜態html出錯!"),CMS_COURSE_PERVIEWISNULL(false,24007,"預覽頁面為空!");//操作代碼boolean success;//操作代碼int code;//提示信息String message;private CmsCode(boolean success, int code, String message){this.success = success;this.code = code;this.message = message;}@Overridepublic boolean success() {return success;}@Overridepublic int code() {return code;}@Overridepublic String message() {return message;}
}
其中繼承的接口ResultCode如下:
package com.xuecheng.framework.model.response;/*** Created by mrt on 2018/3/5.* 10000-- 通用錯誤代碼* 22000-- 媒資錯誤代碼* 23000-- 用戶中心錯誤代碼* 24000-- cms錯誤代碼* 25000-- 文件系統*/
public interface ResultCode {//操作是否成功,true為成功,false操作失敗boolean success();//操作代碼int code();//提示信息String message();}
2)異常處理測試
1.拋出異常
在controller、service、 dao中都可以拋出異常。
修改PageService的add方法,添加拋出異常的代碼
文件位置:com/ xuecheng/ manage_cms/ service/ PageService
/校驗頁面是否存在,根據頁面名稱、站點Id、頁面webpath查詢
CmsPage cmsPage1 =cmsPageRepository.findByPageNameAndSiteIdAndPageWebPath(cmsPage.getPageName(),cmsPage.getSiteId(), cmsPage.getPageWebPath());
if(cmsPage1 !=null){
//校驗頁面是否存在,已存在則拋出異常
ExceptionCast.cast(CmsCode.CMS_ADDPAGE_EXISTS);
}
2、啟動工程,掃描到異常捕獲的類ExceptionCatch
文件位置:com/ xuecheng/ manage_cms/ ManageCmsApplication
在springBoot的啟動類中添加
@ComponentScan(basePackages="com.xuecheng.framework")//掃描common工程下的類
3、前端展示異常信息
服務端響應信息如下:
前端頁面提取異常處理
文件位置:src/ module/ cms/ page/ page_add.vue
addSubmit:function(){this.$refs['pageForm'].validate((valid) => {if (valid) {//表單校驗成功//確認提示this.$confirm('您確認提交嗎?', '提示', { }).then(() => {//調用page_add方法請求服務端的新增頁面接口cmsApi.page_add(this.pageForm).then(res=>{//解析服務端響應內容if(res.success){/*this.$message({message: '提交成功',type: 'success'})*/this.$message.success("提交成功")//將表單清空this.$refs['pageForm'].resetFields();}else if(res.message){this.$message.error(res.message)}else{this.$message.error("提交失敗")}});})}});},
4.不可預知異常處理
1.定義異常捕獲方法
1)異常拋出測試
使用postman測試添加頁面,不輸入cmsPost信息,提交,報錯信息如下:
org.springframework.http.converter.HttpMessageNotReadableException
此異常是springMVC在進行參數轉換時報的錯誤。
具體的響應的信息為:
{
"timestamp": 1528712906727,
"status": 400,
"error": "Bad Request",
"exception": "org.springframework.http.converter.HttpMessageNotReadableException",
"message": "Required request body is missing: public
com.xuecheng.framework.domain.cms.response.CmsPageResult
com.xuecheng.manage_cms.web.controller.CmsPageController.add(com.xuecheng.framework.domain.cms.C
msPage)",
"path": "/cms/page/add"
}
上邊的響應信息在客戶端是無法解析的。
2)異常捕獲方法
針對上邊的問題其解決方案是:
1、我們在map中配置HttpMessageNotReadableException和錯誤代碼。
2、在異常捕獲類中對Exception異常進行捕獲,并從map中獲取異常類型對應的錯誤代碼,如果存在錯誤代碼則返
回此錯誤,否則統一返回99999錯誤。
具體的開發實現如下:
1、在通用錯誤代碼類CommCode中配置非法參數異常
文件位置:com/xuecheng/framework/model/response/CommonCode
INVALID_PARAM(false,10003,"非法參數!")
2.在異常捕獲類中添加對Exception異常的捕獲,并配置 HttpMessageNotReadableException 為非法參數異常
文件位置:com/ xuecheng/ framework/ exception/ ExceptionCatch
package com.xuecheng.framework.exception;import com.google.common.collect.ImmutableMap;
import com.xuecheng.framework.model.response.CommonCode;
import com.xuecheng.framework.model.response.ResponseResult;
import com.xuecheng.framework.model.response.ResultCode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;/*** 統一異常捕獲類* @author Administrator* @version 1.0* @create 2018-09-14 17:32**/
@ControllerAdvice//控制器增強
public class ExceptionCatch {private static final Logger LOGGER = LoggerFactory.getLogger(ExceptionCatch.class);//定義map,配置異常類型所對應的錯誤代碼private static ImmutableMap<Class<? extends Throwable>,ResultCode> EXCEPTIONS;//定義map的builder對象,去構建ImmutableMapprotected static ImmutableMap.Builder<Class<? extends Throwable>,ResultCode> builder = ImmutableMap.builder();//捕獲CustomException此類異常@ExceptionHandler(CustomException.class)@ResponseBodypublic ResponseResult customException(CustomException customException){//記錄日志LOGGER.error("catch exception:{}",customException.getMessage());ResultCode resultCode = customException.getResultCode();return new ResponseResult(resultCode);}//捕獲Exception此類異常@ExceptionHandler(Exception.class)@ResponseBodypublic ResponseResult exception(Exception exception){//記錄日志LOGGER.error("catch exception:{}",exception.getMessage());if(EXCEPTIONS == null){EXCEPTIONS = builder.build();//EXCEPTIONS構建成功}//從EXCEPTIONS中找異常類型所對應的錯誤代碼,如果找到了將錯誤代碼響應給用戶,如果找不到給用戶響應99999異常ResultCode resultCode = EXCEPTIONS.get(exception.getClass());if(resultCode !=null){return new ResponseResult(resultCode);}else{//返回99999異常return new ResponseResult(CommonCode.SERVER_ERROR);}}static {//定義異常類型所對應的錯誤代碼builder.put(HttpMessageNotReadableException.class,CommonCode.INVALID_PARAM);}
}