前言
軟件開發(fā)springboot項(xiàng)目過(guò)程中,不可避免的需要處理各種異常,spring mvc 架構(gòu)中各層會(huì)出現(xiàn)大量的try {...} catch {...} finally {...}代碼塊,不僅有大量的冗余代碼,而且還影響代碼的可讀性。這樣就需要定義個(gè)全局統(tǒng)一異常處理器,以便業(yè)務(wù)層再也不必處理異常。
推薦理由
- 代碼復(fù)制到項(xiàng)目中通過(guò)簡(jiǎn)單的配置即可實(shí)現(xiàn)
- 可以靈活的根據(jù)自己的業(yè)務(wù)異常進(jìn)行更細(xì)粒度的擴(kuò)展
1 封裝統(tǒng)一返回結(jié)果類實(shí)踐
源代碼publicclassAjaxResult{ //是否成功 privateBooleansuccess; //狀態(tài)碼 privateIntegercode; //提示信息 privateStringmsg; //數(shù)據(jù) privateObjectdata; publicAjaxResult(){ } //自定義返回結(jié)果的構(gòu)造方法 publicAjaxResult(Booleansuccess,Integercode,Stringmsg,Objectdata){ this.success=success; this.code=code; this.msg=msg; this.data=data; } //自定義異常返回的結(jié)果 publicstaticAjaxResultdefineError(BusinessExceptionde){ AjaxResultresult=newAjaxResult(); result.setSuccess(false); result.setCode(de.getErrorCode()); result.setMsg(de.getErrorMsg()); result.setData(null); returnresult; } //其他異常處理方法返回的結(jié)果 publicstaticAjaxResultotherError(ErrorEnumerrorEnum){ AjaxResultresult=newAjaxResult(); result.setMsg(errorEnum.getErrorMsg()); result.setCode(errorEnum.getErrorCode()); result.setSuccess(false); result.setData(null); returnresult; } publicBooleangetSuccess(){ returnsuccess; } publicvoidsetSuccess(Booleansuccess){ this.success=success; } publicIntegergetCode(){ returncode; } publicvoidsetCode(Integercode){ this.code=code; } publicStringgetMsg(){ returnmsg; } publicvoidsetMsg(Stringmsg){ this.msg=msg; } publicObjectgetData(){ returndata; } publicvoidsetData(Objectdata){ this.data=data; } }
2 自定義異常封裝類
源碼:publicclassBusinessExceptionextendsRuntimeException{ privatestaticfinallongserialVersionUID=1L; /** *錯(cuò)誤狀態(tài)碼 */ protectedIntegererrorCode; /** *錯(cuò)誤提示 */ protectedStringerrorMsg; publicBusinessException(){ } publicBusinessException(IntegererrorCode,StringerrorMsg){ this.errorCode=errorCode; this.errorMsg=errorMsg; } publicIntegergetErrorCode(){ returnerrorCode; } publicvoidsetErrorCode(IntegererrorCode){ this.errorCode=errorCode; } publicStringgetErrorMsg(){ returnerrorMsg; } publicvoidsetErrorMsg(StringerrorMsg){ this.errorMsg=errorMsg; } }
3 錯(cuò)誤枚舉,拒絕硬編碼
源碼publicenumErrorEnum{ //數(shù)據(jù)操作錯(cuò)誤定義 SUCCESS(200,"成功"), NO_PERMISSION(403,"你沒得權(quán)限"), NO_AUTH(401,"未登錄"), NOT_FOUND(404,"未找到該資源!"), INTERNAL_SERVER_ERROR(500,"服務(wù)器異常請(qǐng)聯(lián)系管理員"), ; /**錯(cuò)誤碼*/ privateIntegererrorCode; /**錯(cuò)誤信息*/ privateStringerrorMsg; ErrorEnum(IntegererrorCode,StringerrorMsg){ this.errorCode=errorCode; this.errorMsg=errorMsg; } publicIntegergetErrorCode(){ returnerrorCode; } publicStringgetErrorMsg(){ returnerrorMsg; } }
4 全局異常處理類
源碼/** *全局異常處理器 * */ @RestControllerAdvice publicclassGlobalExceptionHandler { privatestaticfinalLoggerlog=LoggerFactory.getLogger(GlobalExceptionHandler.class); /** *處理自定義異常 */ @ExceptionHandler(value=BusinessException.class) publicAjaxResultbizExceptionHandler(BusinessExceptione){ log.error(e.getMessage(),e); returnAjaxResult.defineError(e); } /** *處理其他異常 */ @ExceptionHandler(value=Exception.class) publicAjaxResultexceptionHandler(Exceptione){ log.error(e.getMessage(),e); returnAjaxResult.otherError(ErrorEnum.INTERNAL_SERVER_ERROR); } }
5 測(cè)試

返回結(jié)果:

審核編輯 :李倩
-
軟件開發(fā)
+關(guān)注
關(guān)注
0文章
658瀏覽量
29667 -
代碼
+關(guān)注
關(guān)注
30文章
4924瀏覽量
72415 -
架構(gòu)
+關(guān)注
關(guān)注
1文章
532瀏覽量
26442
原文標(biāo)題:公司這套架構(gòu)統(tǒng)一處理 try...catch 真香!
文章出處:【微信號(hào):AndroidPush,微信公眾號(hào):Android編程精選】歡迎添加關(guān)注!文章轉(zhuǎn)載請(qǐng)注明出處。
發(fā)布評(píng)論請(qǐng)先 登錄
國(guó)產(chǎn)信創(chuàng)工業(yè)計(jì)算機(jī)海光3350處理器集特IPC-660工控機(jī)
國(guó)民技術(shù)能否開展一個(gè)M7處理器的試用活動(dòng)。
瑞芯微RK3562處理器的基本特性
自主創(chuàng)新,安全可控:申威SW831處理器與國(guó)產(chǎn)終端產(chǎn)品推薦
集特臺(tái)式機(jī)GPC-100:搭載海光3350處理器,國(guó)芯替代的辦公新選擇
集特工控機(jī)搭載海光3350處理器:國(guó)產(chǎn)化工業(yè)計(jì)算的創(chuàng)新實(shí)踐
RK3328處理器核心特性概述
RK3308處理器核心特性解析
RV1109處理器概述
TPS65911A 66AK2G12處理器用戶指南
面向NXP i.MX8處理器的電源解決方案
TPS659118 66AK2G02處理器用戶指南

公司這套架構(gòu)統(tǒng)一處理try...catch真香!
評(píng)論