您好,登錄后才能下訂單哦!
今天小編給大家分享一下怎么使用aop實(shí)現(xiàn)全局異常處理的相關(guān)知識(shí)點(diǎn),內(nèi)容詳細(xì),邏輯清晰,相信大部分人都還太了解這方面的知識(shí),所以分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后有所收獲,下面我們一起來(lái)了解一下吧。
日常業(yè)務(wù)中存在的問(wèn)題
使用大量的try/catch來(lái)捕獲異常
導(dǎo)致整個(gè)控制層代碼可讀性極差,并且此類工作重復(fù)枯燥、容易復(fù)制錯(cuò)。
一份糟糕的控制器代碼如下:@RequestMapping("test/run/old")
public JsonResponse testRunOld() { try { exampleService.runTest(); System.out.println("正常運(yùn)行"); return JsonResponse.newOk(); }catch (DataNotCompleteException e) { logger.error("something error occured!"); return JsonResponse.newError(ErrorMsgEnum.DATA_NO_COMPLETE); } catch (Exception e) { return JsonResponse.newError(); } }
我們要把代碼變成這樣:
@Controller public class TestController { @Autowired private IExampleService exampleService; @RequestMapping("test/run/aop") public JsonResponse testRunAop() throws Exception { exampleService.runTest(); System.out.println("正常運(yùn)行"); return JsonResponse.newOk(); } }
@Service public class ExampleService implements IExampleService{ @Override public void runTest() throws Exception { // do something System.out.println("run something"); throw new CustomException(ErrorMsgEnum.DATA_NO_COMPLETE); } }
這樣做以后,代碼里少了很多try和catch,這些到處復(fù)制的代碼本來(lái)就應(yīng)該統(tǒng)一起來(lái),只是在aop以前沒有什么更好的處理方式,只能復(fù)制。
其次,service拋出異常后,不用再去controller里加一段catch,這種操作每次都要浪費(fèi)5-15秒(如果你不熟悉IDE中的快捷鍵,這就是噩夢(mèng))
現(xiàn)在你的異常只要往上拋出去就不管了(throws Exception),可以專心寫業(yè)務(wù)代碼
如何完成?其實(shí)原理相當(dāng)簡(jiǎn)單。
把那些煩人的try丟到AOP中處理
我們將采用Spring AOP統(tǒng)一處理異常,統(tǒng)一返回后端接口的結(jié)果。
使用一個(gè)自定義異常和一個(gè)錯(cuò)誤前端提示枚舉來(lái)逐層傳遞消息
一個(gè)錯(cuò)誤枚舉來(lái)代替新建異常信息類,減少業(yè)務(wù)異常信息文件的數(shù)量
幾個(gè)核心類代碼
//正常返回的枚舉 SUCCESS(true, 2000,"正常返回", "操作成功"), // 系統(tǒng)錯(cuò)誤,50開頭 SYS_ERROR(false, 5000, "系統(tǒng)錯(cuò)誤", "親,系統(tǒng)出錯(cuò)了哦~"), PARAM_INVILAD(false, 5001, "參數(shù)出現(xiàn)異常", "參數(shù)出現(xiàn)異常"), DATA_NO_COMPLETE(false, 5002, "數(shù)據(jù)填寫不完整,請(qǐng)檢查", "數(shù)據(jù)填寫不完整,請(qǐng)檢查"); private ErrorMsgEnum(boolean ok, int code, String msg ,String userMsg) { this.ok = ok; this.code = code; this.msg = msg; this.userMsg = userMsg; } private boolean ok; private int code; private String msg; private String userMsg; }
控制層返回結(jié)果POJO類
public class JsonResponse{ String msg; Object data; public JsonResponse() { msg = ""; data = null; } public static JsonResponse newOk() { JsonResponse response = new JsonResponse(); response.setState(State.newOk()); return response; } public static JsonResponse newOk(Object data) { JsonResponse response = new JsonResponse(); response.setData(data); response.setState(State.newOk()); return response; } public static JsonResponse newError() { JsonResponse response = new JsonResponse(); response.setMsg("無(wú)情的系統(tǒng)異常!"); return response; } public static JsonResponse newError(ErrorMsgEnum errorMsgEnum) { JsonResponse response = new JsonResponse(); state.setMsg(errorMsgEnum.getErrorMsg()); return response; } }
自定義異常類
public class CustomException extends Exception { private ErrorMsgEnum errorMsgEnum; public CustomException(ErrorMsgEnum errorMsgEnum) { this.errorMsgEnum = errorMsgEnum; } }
AOP捕獲異常處理類
@Around("execution(public * com.jason.*.controller..*.*(..))") public JsonResponse serviceAOP(ProceedingJoinPoint pjp) throws Exception { JsonResponse newResultVo = null; try { return (JsonResponse) pjp.proceed(); } catch (CustomException e) { logger.info("自定義業(yè)務(wù)異常:" + e.getMessage()); ErrorMsgEnum errorMsgEnum = e.getErrorMsgEnum(); if (Objects.nonNull(errorMsgEnum)) { newResultVo = JsonResponse.newError(errorMsgEnum); } else { newResultVo = JsonResponse.newError(e.getMessage()); } } catch (Exception e) { //可以順便處理你的日志,此處能取到方法名,參數(shù)等等 logger.error("出現(xiàn)運(yùn)行時(shí)異常:", e); newResultVo = JsonResponse.newError(); } return newResultVo; }
Test && End
至此,我們已經(jīng)可以直接在 Service 或 Controller 中隨意拋出一個(gè)異常,
直接每個(gè)控制器方法拋出的異常定義為 throws Exception 即可
經(jīng)過(guò)這次處理:
最大的好處是:沒有try
異常處理和返回結(jié)果得到統(tǒng)一,不怕你的隊(duì)友復(fù)制錯(cuò)了。
以上就是“怎么使用aop實(shí)現(xiàn)全局異常處理”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家閱讀完這篇文章都有很大的收獲,小編每天都會(huì)為大家更新不同的知識(shí),如果還想學(xué)習(xí)更多的知識(shí),請(qǐng)關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。