前言
軟件開(kāi)發(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é)果類(lèi)實(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 自定義異常封裝類(lèi)

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,"你沒(méi)得權(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 全局異常處理類(lèi)

/** *全局異常處理器 * */ @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é)果:

審核編輯 :李倩
-
軟件開(kāi)發(fā)
+關(guān)注
關(guān)注
0文章
639瀏覽量
28398 -
代碼
+關(guān)注
關(guān)注
30文章
4894瀏覽量
70480 -
架構(gòu)
+關(guān)注
關(guān)注
1文章
528瀏覽量
25929
原文標(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ó)民技術(shù)能否開(kāi)展一個(gè)M7處理器的試用活動(dòng)。
自主創(chuàng)新,安全可控:申威SW831處理器與國(guó)產(chǎn)終端產(chǎn)品推薦
RV1109處理器概述
面向NXP i.MX8處理器的電源解決方案

TPS659118 66AK2G02處理器用戶(hù)指南

SK-AM68處理器入門(mén)套件用戶(hù)指南

AM69處理器入門(mén)套件用戶(hù)指南

ADS8361與TMS470處理器的接口

ADS786x與TMS470處理器的接口

運(yùn)放的調(diào)零處理方法有哪些?
恩智浦i.MX8處理器的集成電源設(shè)計(jì)

基于全志V853處理器的智能輔助駕駛算法介紹

使用TPS6521815 PMIC為NXP i.MX 7處理器供電

評(píng)論