欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

springboot?全局異常處理和統(tǒng)一響應(yīng)對象的處理方式

 更新時間:2022年06月28日 14:35:09   作者:豬仔讀我名字  
這篇文章主要介紹了springboot?全局異常處理和統(tǒng)一響應(yīng)對象,主要包括SpringBoot 默認(rèn)的異常處理機(jī)制和SpringBoot 全局異常處理,本文給大家介紹的非常詳細(xì),需要的朋友可以參考下

springboot異常處理

SpringBoot 默認(rèn)的異常處理機(jī)制

默認(rèn)情況,SpringBoot 提供兩種不同響應(yīng)方式

  • 一種是瀏覽器客戶端請求一個不存在的頁面或服務(wù)端異常時,SpringBoot默認(rèn)會響應(yīng)一個html
  • 另一種是使用postman等調(diào)試工具請求不存在的url或服務(wù)端異常時,默認(rèn)返回json信息

SpringBoot 全局異常處理

一般我們不會將錯誤信息返回前端,自己去try catch捕獲異常,但有個問題:每個方法都這樣捕獲異常,肯定是不合適,這是我們就需要全局的異常處理了。

@RestController
public class ExceptionController {
    @GetMapping("exceptionA")
    public void methodA() {
        try {
            int a = 100 / 0;
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

1. 局部異常處理

使用@EceptionHandle注解實現(xiàn)某個類的局部異常處理

   @RestController
   public class ExceptionController {
   
       @GetMapping("exceptionA")
       public void methodA() {
           int a = 100 / 0;
       }
   
       /**
        * 局部異常處理
        */
       @ExceptionHandler(Exception.class)
       public String exHandler(Exception e) {
           // 判斷發(fā)生異常的類型是除0異常則做出響應(yīng)
           if (e instanceof ArithmeticException) {
               return "發(fā)生了除0異常";
           }
           // 未知的異常做出響應(yīng)
           return "發(fā)生了未知異常";
       }
   }

2. 全局異常處理

使用@ControllerAdvice +@ExceptionHandler注解實現(xiàn)全局異常處理

自定義一個異常類

@RestControllerAdvice
public class DefaultException {
?
    @ExceptionHandler({NullPointerException.class})
    public String exception(NullPointerException exception) {
        return "空指針異常";
?
    }
?
    @ExceptionHandler({IndexOutOfBoundsException.class})
    public String exception(IndexOutOfBoundsException exception) {
        return "數(shù)組越界異常";
    }
}

增加一個異常方法測試,由于局部異常優(yōu)先級更高先注釋掉了

@RestController
public class ExceptionController {
?
    @GetMapping("exceptionA")
    public void methodA() {
        int a = 100 / 0;
    }
?
    @GetMapping("exceptionB")
    public void methodB() {
        List list = new ArrayList<>();
        System.out.println(list.get(0));
    }
?
    /**
     * 局部異常處理
     */
    //@ExceptionHandler(Exception.class)
    //public String exHandler(Exception e) {
    //    // 判斷發(fā)生異常的類型是除0異常則做出響應(yīng)
    //    if (e instanceof ArithmeticException) {
    //        return "發(fā)生了除0異常";
    //    }
    //    // 未知的異常做出響應(yīng)
    //    return "發(fā)生了未知異常";
    //}
}

全局異常注解已生效

自定義異常

自定義異常只需要繼承exception類或其子類

@Data
@NoArgsConstructor
public class CustomException extends Exception {
?
    private static final long serialVersionUID = 1L;
?
    private Integer code;
?
    private String mes;
?
    /**
     * @param code 狀態(tài)碼
     * @param msg  異常返回信息
     * @description 構(gòu)造器
     */
    public CustomException(Integer code, String msg) {
        super(msg);
        this.code = code;
    }
}

使用時可以直接拋出異常對象

@GetMapping("exceptionC")
public void methodC() throws CustomException {
    int a = 1;
    if (a == 1) {
        throw new CustomException(10086, "自定義異常");
    }
}

統(tǒng)一響應(yīng)對象

實際開發(fā)中我們需要封裝統(tǒng)一的響應(yīng)對象,區(qū)分狀態(tài)碼和信息,以便前端處理。

定義統(tǒng)一的響應(yīng)對象

一般包含狀態(tài)碼,錯誤信息,數(shù)據(jù)等。

自定義一些方法用來返回信息,比如我定義的success(),failed()方法

@Data
@NoArgsConstructor
@AllArgsConstructor
public class R<T> {
    /**
     * 返回狀態(tài)碼
     */
    private Integer code;
    /**
     * 返回信息
     */
    private String msg;
    /**
     * 數(shù)據(jù)
     */
    private T data;
?
    public static R success() {
        return new R(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMsg(), null);
    }
?
    public static R success(Object data) {
        return new R(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMsg(), data);
    }
?
    public static R failed() {
        return new R(ResultCode.FAILED.getCode(), ResultCode.FAILED.getMsg(), null);
    }
?
    public static R failed(String msg) {
        return new R(ResultCode.FAILED.getCode(), msg, null);
    }
?
    public static R failed(int code, String msg) {
        return new R(code, msg, null);
    }
}

枚舉信息

枚舉一些常用的狀態(tài)信息

我就舉個例子,只枚舉2個,根據(jù)需要去自定義

@NoArgsConstructor
@AllArgsConstructor
public enum ResultCode {
?
    SUCCESS(200, "請求成功"),
    FAILED(500, "服務(wù)器錯誤");
?
    private int code;
    private String msg;
?
    public int getCode() {
        return code;
    }
?
    public void setCode(int code) {
        this.code = code;
    }
?
    public String getMsg() {
        return msg;
    }
?
    public void setMsg(String msg) {
        this.msg = msg;
    }
}

響應(yīng)對象

使用時直接返回定義的對象類型就行了,將定義的全局異常返回類型也改成統(tǒng)一的響應(yīng)對象

@RestControllerAdvice
public class DefaultException {
    @ExceptionHandler({CustomException.class})
    public R exception(CustomException e) {
        return R.failed(e.getCode(),e.getMessage());
    }

    @ExceptionHandler({Exception.class})
    public R exception(Exception e) {
        return R.failed(e.getMessage());
    }
}

全局異常和響應(yīng)對象的簡單介紹就這樣了,歡迎補(bǔ)充指正。

gitee地址:gitee.com/rainscloud/…

到此這篇關(guān)于springboot 全局異常處理和統(tǒng)一響應(yīng)對象的文章就介紹到這了,更多相關(guān)springboot全局異常處理內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論