前言
軟件開發springboot項目過程中,不可避免的需要處理各種異常,spring mvc 架構中各層會出現大量的try {...} catch {...} finally {...}代碼塊,不僅有大量的冗余代碼,而且還影響代碼的可讀性。這樣就需要定義個全局統一異常處理器,以便業務層再也不必處理異常。
推薦理由
- 代碼復制到項目中通過簡單的配置即可實現
- 可以靈活的根據自己的業務異常進行更細粒度的擴展
1 封裝統一返回結果類實踐

publicclassAjaxResult{ //是否成功 privateBooleansuccess; //狀態碼 privateIntegercode; //提示信息 privateStringmsg; //數據 privateObjectdata; publicAjaxResult(){ } //自定義返回結果的構造方法 publicAjaxResult(Booleansuccess,Integercode,Stringmsg,Objectdata){ this.success=success; this.code=code; this.msg=msg; this.data=data; } //自定義異常返回的結果 publicstaticAjaxResultdefineError(BusinessExceptionde){ AjaxResultresult=newAjaxResult(); result.setSuccess(false); result.setCode(de.getErrorCode()); result.setMsg(de.getErrorMsg()); result.setData(null); returnresult; } //其他異常處理方法返回的結果 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; /** *錯誤狀態碼 */ protectedIntegererrorCode; /** *錯誤提示 */ 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 錯誤枚舉,拒絕硬編碼

publicenumErrorEnum{ //數據操作錯誤定義 SUCCESS(200,"成功"), NO_PERMISSION(403,"你沒得權限"), NO_AUTH(401,"未登錄"), NOT_FOUND(404,"未找到該資源!"), INTERNAL_SERVER_ERROR(500,"服務器異常請聯系管理員"), ; /**錯誤碼*/ privateIntegererrorCode; /**錯誤信息*/ 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 測試

返回結果:

審核編輯 :李倩
聲明:本文內容及配圖由入駐作者撰寫或者入駐合作網站授權轉載。文章觀點僅代表作者本人,不代表電子發燒友網立場。文章及其配圖僅供工程師學習之用,如有內容侵權或者其他違規問題,請聯系本站處理。
舉報投訴
-
軟件開發
+關注
關注
0文章
636瀏覽量
27983 -
代碼
+關注
關注
30文章
4876瀏覽量
69963 -
架構
+關注
關注
1文章
525瀏覽量
25794
原文標題:公司這套架構統一處理 try...catch 真香!
文章出處:【微信號:AndroidPush,微信公眾號:Android編程精選】歡迎添加關注!文章轉載請注明出處。
發布評論請先 登錄
相關推薦
自主創新,安全可控:申威SW831處理器與國產終端產品推薦
動力。本文將從申威SW831處理器的技術特性出發,推薦兩款基于該處理器的終端設備—— GPC-100申威臺式機 和 G701申威瘦客戶機 ,為政企、金融、能源等關鍵領域提供安全可靠的國產化解決方案。 一、申威SW831
RV1109處理器概述
RV1109處理器是一款集成了先進技術的高性能芯片,其主要特性彰顯了在多個領域的強大應用能力。 該處理器搭載了雙核設計,結合了ARM Cortex-A7處理器核心與RISC-V MCU
基于全志V853處理器的智能輔助駕駛算法介紹
全志V853處理器,是專為智慧視覺領域設計的AI處理器,配備了高效的NPU算力和豐富的外設接口,能夠提供穩定的AI邊緣計算支持;同時,基于V853處理器這一性能優勢,全志還為客戶提供了

使用TPS6521815 PMIC為NXP i.MX 7處理器供電
電子發燒友網站提供《使用TPS6521815 PMIC為NXP i.MX 7處理器供電.pdf》資料免費下載
發表于 09-13 09:40
?0次下載

高通驍龍6 Gen 3處理器發布
高通公司近日正式推出了驍龍6 Gen 3處理器,這款芯片采用先進的三星4nm工藝打造,代號為SM6475-AB,標志著中端處理器市場的新一輪性能革新。
一站式統一返回值封裝、異常處理、異常錯誤碼解決方案—最強的Sping Boot接口優雅響應處理器
處理的邏輯集中到一個地方,避免代碼中出現大量的try-catch語句,降低了代碼的復雜度,提高了代碼的可讀性;異常體系的設計可以清晰地區分不同類型的異常,使得開發者能夠更加精準地處理異
龍芯CPU統一系統架構規范及參考設計下載
*附件:LoongArch 系統調用(syscall)ABI.pdf
*附件:龍芯 CPU 統一系統架構規范(適用于 LA 架構通用 PC、服務器系列)-v4.1.0.pdf
*附件:龍芯CPU
發表于 06-20 14:42
評論