<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          統(tǒng)一處理 try...catch 這么香,求求你不要再滿屏寫了,再發(fā)現(xiàn)扣績效!

          共 3733字,需瀏覽 8分鐘

           ·

          2021-07-20 23:11

          點(diǎn)擊“開發(fā)者技術(shù)前線”,選擇“星標(biāo)??”

          讓一部分開發(fā)者看到未來

          1fd88ca3752803691a05625dcc6c6feb.webp

          責(zé)編:樂樂?|?來自:小李子說程序

          鏈接:urlify.cn/6naQjq


          前言

          軟件開發(fā)springboot項(xiàng)目過程中,不可避免的需要處理各種異常,spring mvc 架構(gòu)中各層會(huì)出現(xiàn)大量的try {...} catch {...} finally {...}代碼塊,不僅有大量的冗余代碼,而且還影響代碼的可讀性。這樣就需要定義個(gè)全局統(tǒng)一異常處理器,以便業(yè)務(wù)層再也不必處理異常。

          推薦理由

          • 代碼復(fù)制到項(xiàng)目中通過簡單的配置即可實(shí)現(xiàn)

          • 可以靈活的根據(jù)自己的業(yè)務(wù)異常進(jìn)行更細(xì)粒度的擴(kuò)展

          實(shí)踐

          1 封裝統(tǒng)一返回結(jié)果類

          2bbe1f3bce0ed69aa9941cb6ff54dc4b.webp

          源代碼

          public?class?AjaxResult?{
          ?//是否成功
          ????private?Boolean?success;
          ????//狀態(tài)碼
          ????private?Integer?code;
          ????//提示信息
          ????private?String?msg;
          ????//數(shù)據(jù)
          ????private?Object?data;
          ????public?AjaxResult()?{

          ????}
          ????//自定義返回結(jié)果的構(gòu)造方法
          ????public?AjaxResult(Boolean?success,Integer?code,?String?msg,Object?data)?{
          ????????this.success?=?success;
          ????????this.code?=?code;
          ????????this.msg?=?msg;
          ????????this.data?=?data;
          ????}
          ????//自定義異常返回的結(jié)果
          ????public?static?AjaxResult?defineError(BusinessException?de){
          ?????AjaxResult?result?=?new?AjaxResult();
          ????????result.setSuccess(false);
          ????????result.setCode(de.getErrorCode());
          ????????result.setMsg(de.getErrorMsg());
          ????????result.setData(null);
          ????????return?result;
          ????}
          ????//其他異常處理方法返回的結(jié)果
          ????public?static?AjaxResult?otherError(ErrorEnum?errorEnum){
          ?????AjaxResult?result?=?new?AjaxResult();
          ????????result.setMsg(errorEnum.getErrorMsg());
          ????????result.setCode(errorEnum.getErrorCode());
          ????????result.setSuccess(false);
          ????????result.setData(null);
          ????????return?result;
          ????}
          ?public?Boolean?getSuccess()?{
          ??return?success;
          ?}
          ?public?void?setSuccess(Boolean?success)?{
          ??this.success?=?success;
          ?}
          ?public?Integer?getCode()?{
          ??return?code;
          ?}
          ?public?void?setCode(Integer?code)?{
          ??this.code?=?code;
          ?}
          ?public?String?getMsg()?{
          ??return?msg;
          ?}
          ?public?void?setMsg(String?msg)?{
          ??this.msg?=?msg;
          ?}
          ?public?Object?getData()?{
          ??return?data;
          ?}
          ?public?void?setData(Object?data)?{
          ??this.data?=?data;
          ?}
          ????
          }

          2 自定義異常封裝類

          b5099a6df4546953633716d777e6033d.webp

          源碼:

          public?class?BusinessException?extends?RuntimeException?{
          ?private?static?final?long?serialVersionUID?=?1L;
          ?/**
          ??*?錯(cuò)誤狀態(tài)碼
          ??*/

          ?protected?Integer?errorCode;
          ?/**
          ??*?錯(cuò)誤提示
          ??*/

          ?protected?String?errorMsg;

          ?public?BusinessException(){

          ?????}

          ?public?BusinessException(Integer?errorCode,?String?errorMsg)?{
          ?????????this.errorCode?=?errorCode;
          ?????????this.errorMsg?=?errorMsg;
          ?????}

          ?public?Integer?getErrorCode()?{
          ??return?errorCode;
          ?}

          ?public?void?setErrorCode(Integer?errorCode)?{
          ??this.errorCode?=?errorCode;
          ?}

          ?public?String?getErrorMsg()?{
          ??return?errorMsg;
          ?}

          ?public?void?setErrorMsg(String?errorMsg)?{
          ??this.errorMsg?=?errorMsg;
          ?}
          }

          3 錯(cuò)誤枚舉,拒絕硬編碼

          c063bbc3136ccb6951ef88a77088fdca.webp

          源碼

          搜索公眾號(hào)后端架構(gòu)師后臺(tái)回復(fù)“架構(gòu)整潔”,獲取一份驚喜禮包。

          public?enum?ErrorEnum?{
          ?//?數(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ò)誤碼?*/
          ?private?Integer?errorCode;

          ?/**?錯(cuò)誤信息?*/
          ?private?String?errorMsg;

          ?ErrorEnum(Integer?errorCode,?String?errorMsg)?{
          ??this.errorCode?=?errorCode;
          ??this.errorMsg?=?errorMsg;
          ?}

          ????public?Integer?getErrorCode()?{
          ????????return?errorCode;
          ????}

          ????public?String?getErrorMsg()?{
          ????????return?errorMsg;
          ????}
          }

          4 全局異常處理類

          43db35468c965e1d701d0a0f0dbbd8e7.webp

          源碼

          /**
          ?*?全局異常處理器
          ?*?
          ?*/

          @RestControllerAdvice
          public?class?GlobalExceptionHandler
          {
          ????private?static?final?Logger?log?=?LoggerFactory.getLogger(GlobalExceptionHandler.class);



          ????/**
          ?????*?處理自定義異常
          ?????*
          ?????*/

          ????@ExceptionHandler(value?=?BusinessException.class)
          ????public?AjaxResult?bizExceptionHandler(BusinessException?e)?
          {
          ?????log.error(e.getMessage(),?e);
          ????????return?AjaxResult.defineError(e);
          ????}

          ????/**
          ?????*處理其他異常
          ?????*
          ?????*/

          ????@ExceptionHandler(value?=?Exception.class)
          ????public?AjaxResult?exceptionHandler(?Exception?e)?
          {
          ??????log.error(e.getMessage(),?e);
          ????????return?AjaxResult.otherError(ErrorEnum.INTERNAL_SERVER_ERROR);
          ???????
          ????}

          }

          5 測(cè)試

          9bd62f3d0f41081136664e27e1766ea3.webp

          返回結(jié)果:

          c59259b0e8b55af57f2f32f349e7b3ec.webp

          —??—

          點(diǎn)這里??關(guān)注我,記得標(biāo)星呀~


          前線推出學(xué)習(xí)交流一定要備注:研究/工作方向+地點(diǎn)+學(xué)校/公司+昵稱(如JAVA+上海

          掃碼加小編微信,進(jìn)群和大佬們零距離



          END

          后臺(tái)回復(fù)“電子書”?“資料”?領(lǐng)取一份干貨,數(shù)百面試手冊(cè)等
          歷史推薦


          谷歌內(nèi)部代碼評(píng)審規(guī)范完整版出爐!

          三款主流的 JSON 解析庫性能大比拼,到底誰最牛?

          API設(shè)計(jì)得好,下班下得早


          好文點(diǎn)個(gè)在看吧!
          瀏覽 58
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          評(píng)論
          圖片
          表情
          推薦
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  日韩精品无码一级 | 无码毛片一区二区三区视频免费看 | 国产成人精品视频免费看 | 亚洲天堂日韩国 | 欧美午夜性爱视频 |