<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                ## 一、全局異常處理器 ControllerAdvice注解的作用就是監聽所有的Controller,一旦Controller拋出CustomException,就會在@ExceptionHandler(CustomException.class)對該異常進行處理。 ![](https://img.kancloud.cn/d7/a4/d7a4ee6c1db665009bacf6dd05929271_434x217.png) ~~~ @ControllerAdvice public class WebExceptionHandler { @ExceptionHandler(CustomException.class) @ResponseBody public AjaxResponse customerException(CustomException e) { if(e.getCode() == CustomExceptionType.SYSTEM_ERROR.getCode()){ //400異常不需要持久化,將異常信息以友好的方式告知用戶就可以 //TODO 將500異常信息持久化處理,方便運維人員處理 } return AjaxResponse.error(e); } @ExceptionHandler(Exception.class) @ResponseBody public AjaxResponse exception(Exception e) { //TODO 將異常信息持久化處理,方便運維人員處理 //沒有被程序員發現,并轉換為CustomException的異常,都是其他異常或者未知異常. return AjaxResponse.error(new CustomException(CustomExceptionType.OTHER_ERROR,"未知異常")); } } ~~~ ## 二、人為制造異常測試一下 ![](https://img.kancloud.cn/22/f6/22f6548f7f36ca2927503a27f532fb26_494x202.png) ~~~ @Service public class ExceptionService { //服務層,模擬系統異常 public void systemBizError() throws CustomException { try { Class.forName("com.mysql.jdbc.xxxx.Driver"); } catch (ClassNotFoundException e) { throw new CustomException(CustomExceptionType.SYSTEM_ERROR,"在XXX業務,myBiz()方法內,出現ClassNotFoundException"); } } //服務層,模擬用戶輸入數據導致的校驗異常 public List<String> userBizError(int input) throws CustomException { if(input < 0){ //模擬業務校驗失敗邏輯 throw new CustomException(CustomExceptionType.USER_INPUT_ERROR,"您輸入的數據不符合業務邏輯,請確認后重新輸入!"); }else{ // List<String> list = new ArrayList<>(); list.add("科比"); list.add("詹姆斯"); list.add("庫里"); return list; } } } ~~~ 在HelloController里面測試 引用全局異常處理 ~~~ @Resource ExceptionService exceptionService; ~~~ 引入測試 ~~~ @RequestMapping("/ex/service") public @ResponseBody AjaxResponse system(){ exceptionService.systemBizError(); return AjaxResponse.success(); } @RequestMapping("/ex/user") public @ResponseBody AjaxResponse user(Integer input){ //請求成功返回數據 return AjaxResponse.success(exceptionService.userBizError(input)); } ~~~ 在WebExceptionHandler中error打斷點 ![](https://img.kancloud.cn/e5/06/e5064f01e32dbb00fbd174187b220415_1054x311.png) 在postman中執行測試,會發現異常被捕獲 ![](https://img.kancloud.cn/8f/f0/8ff0e7983cf6599828368061388fdddb_1438x704.png) 繼續執行發現前端接收到異常信息 ![](https://img.kancloud.cn/ab/25/ab25a577d129652d7bba5409f9e4edc1_1219x662.png) 同理,測試有參數傳遞的方法也可以 ![](https://img.kancloud.cn/f4/54/f45487067467051a53390181f1366586_1414x687.png) 無異常反饋如下 ![](https://img.kancloud.cn/fc/ef/fcef6b2262dd89967848b29fbc94cd8b_988x772.png) 可以看到輸入正確的值不會捕捉異常,錯誤的值會被捕捉異常并按照規定的方式給出
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看