您好,登錄后才能下訂單哦!
這篇文章主要介紹了SpringMVC項(xiàng)目異常處理機(jī)制實(shí)例代碼分析的相關(guān)知識(shí),內(nèi)容詳細(xì)易懂,操作簡單快捷,具有一定借鑒價(jià)值,相信大家閱讀完這篇SpringMVC項(xiàng)目異常處理機(jī)制實(shí)例代碼分析文章都會(huì)有所收獲,下面我們一起來看看吧。
通常分為三類:系統(tǒng)異常(SystemException),業(yè)務(wù)異常(BusinessException)和其他異常(Exception)
業(yè)務(wù)異常指由于用戶的不規(guī)范操作產(chǎn)生的異常,如不合法的參數(shù)傳入
系統(tǒng)異常指項(xiàng)目運(yùn)行過程中可預(yù)計(jì)但無法避免的異常,如數(shù)據(jù)庫宕機(jī)
其他異常指開發(fā)者未曾預(yù)料到的異常
public class BusinessException extends RuntimeException { private Integer code; public BusinessException(Integer code, String message) { super(message); this.code = code; } public Integer getCode() { return code; } public void setCode(Integer code) { this.code = code; } }
public class SystemException extends RuntimeException { private Integer code; public SystemException(Integer code, String message, Throwable cause) { super(message, cause); this.code = code; } public Integer getCode() { return code; } public void setCode(Integer code) { this.code = code; } }
其他異常是未預(yù)料到會(huì)發(fā)生的異常,除了上面的兩種異常外的所有異常都可以當(dāng)作其他異常,由于所有的異常都繼承自 Exception 類,所以就把 Exception 作為其他異常
提高代碼可讀性,便于開發(fā),自行協(xié)商設(shè)置即可
public class Code { public static final Integer SYSTEM_ERR = 50001; public static final Integer SYSTEM_TIMEOUT_ERR = 50002; public static final Integer SYSTEM_UNKNOWN_ERR = 59999; public static final Integer BUSINESS_ERR = 60001; }
由于系統(tǒng)異常和其他異常需要工作人員對(duì)系統(tǒng)進(jìn)行調(diào)整,所以出現(xiàn)這兩種異常時(shí)通常要記錄進(jìn)日志,并將相關(guān)信息發(fā)送給運(yùn)維人員和開發(fā)人員,而業(yè)務(wù)異常則不需要
@RestControllerAdvice public class ProjectExceptionAdvice { // 攔截處理業(yè)務(wù)異常 @ExceptionHandler(BusinessException.class) public Result doBusinessException(BusinessException ex) { return new Result(ex.getCode(), null, ex.getMessage()); } // 攔截處理系統(tǒng)異常 @ExceptionHandler(SystemException.class) public Result doSystemException(SystemException ex) { // 記錄日志,發(fā)送消息給運(yùn)維人員,發(fā)送郵件給開發(fā)人員 return new Result(ex.getCode(), null, ex.getMessage()); } //攔截處理其他異常 @ExceptionHandler(Exception.class) public Result doException(Exception ex) { // 記錄日志,發(fā)送消息給運(yùn)維人員,發(fā)送郵件給開發(fā)人員 return new Result(Code.SYSTEM_UNKNOWN_ERR, null, "系統(tǒng)繁忙,請(qǐng)聯(lián)系管理員。"); } }
以業(yè)務(wù)層為例,查找 id 小于等于 0 時(shí)認(rèn)為參數(shù)非法,可以拋出業(yè)務(wù)異常。對(duì)于系統(tǒng)異常,使用 try catch 語句包裹可能會(huì)出現(xiàn)異常的語句,捕獲異常并包裝成自定義的異常,再將其拋出
@Service public class BookServiceImpl implements BookService { @Autowired private BookDao bookDao; @Override public Book getById(Integer id) { // id 小于等于零時(shí)拋出業(yè)務(wù)異常 if (id <= 0) { throw new BusinessException(Code.BUSINESS_ERR, "參數(shù)不合法,請(qǐng)檢查傳入?yún)?shù)!"); } // 將可能出現(xiàn)的異常進(jìn)行包裝,裝換成自定義異常 try { int x = 1 / 0; // 加入此行出現(xiàn)異常 return bookDao.getById(id); // 模擬數(shù)據(jù)庫訪問出錯(cuò) } catch (Exception e) { throw new SystemException(Code.SYSTEM_TIMEOUT_ERR, "數(shù)據(jù)庫訪問異常,請(qǐng)稍后重試!", e); } } }
最后可以用 Postman 發(fā)送對(duì)應(yīng)的請(qǐng)求進(jìn)行測(cè)試
關(guān)于“SpringMVC項(xiàng)目異常處理機(jī)制實(shí)例代碼分析”這篇文章的內(nèi)容就介紹到這里,感謝各位的閱讀!相信大家對(duì)“SpringMVC項(xiàng)目異常處理機(jī)制實(shí)例代碼分析”知識(shí)都有一定的了解,大家如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。