溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

如何在SpringBoot中實(shí)現(xiàn)全局異常處理方式

發(fā)布時(shí)間:2021-05-09 10:33:31 來(lái)源:億速云 閱讀:248 作者:Leah 欄目:開發(fā)技術(shù)

如何在SpringBoot中實(shí)現(xiàn)全局異常處理方式?很多新手對(duì)此不是很清楚,為了幫助大家解決這個(gè)難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來(lái)學(xué)習(xí)下,希望你能有所收獲。

springboot是什么

springboot一種全新的編程規(guī)范,其設(shè)計(jì)目的是用來(lái)簡(jiǎn)化新Spring應(yīng)用的初始搭建以及開發(fā)過(guò)程,SpringBoot也是一個(gè)服務(wù)于框架的框架,服務(wù)范圍是簡(jiǎn)化配置文件。

一、全局異常處理方式一

SpringBoot中,@ControllerAdvice 即可開啟全局異常處理,使用該注解表示開啟了全局異常的捕獲,我們只需在自定義一個(gè)方法使用@ExceptionHandler注解然后定義捕獲異常的類型即可對(duì)這些捕獲的異常進(jìn)行統(tǒng)一的處理。

1.1 自定義全局異常類

@ControllerAdvice
public class MyExceptionHandler {

    @ExceptionHandler(value =Exception.class)
    @ResponseBody
    public String exceptionHandler(Exception e){
        System.out.println("全局異常捕獲>>>:"+e);
        return "全局異常捕獲,錯(cuò)誤原因>>>"+e.getMessage();
    }
}

1.2 手動(dòng)拋出異常

 @GetMapping("/getById/{userId}")
public CommonResult<User> getById(@PathVariable Integer userId){
    // 手動(dòng)拋出異常
    int a = 10/0;
    return CommonResult.success(userService.getById(userId));
}

1.3 測(cè)試打印

如何在SpringBoot中實(shí)現(xiàn)全局異常處理方式

如何在SpringBoot中實(shí)現(xiàn)全局異常處理方式

很顯然這樣的用戶體驗(yàn)效果是極差的,雖然這種能夠讓我們知道異常的原因,但是在很多的情況下來(lái)說(shuō),可能還是不夠人性化,不符合我們的要求。

二、全局異常處理方式二

2.1 定義基礎(chǔ)接口類

/**
 * @description: 服務(wù)接口類
 * @author: DT
 * @date: 2021/4/19 21:39
 */
public interface BaseErrorInfoInterface {

    /**
     *  錯(cuò)誤碼
     * @return
     */
    String getResultCode();

    /**
     * 錯(cuò)誤描述
     * @return
     */
    String getResultMsg();
}

2.2 定義枚舉類

/**
 * @description: 異常處理枚舉類
 * @author: DT
 * @date: 2021/4/19 21:41
 * @version: v1.0
 */
public enum ExceptionEnum implements BaseErrorInfoInterface{
    
    // 數(shù)據(jù)操作錯(cuò)誤定義
    SUCCESS("2000", "成功!"),
    BODY_NOT_MATCH("4000","請(qǐng)求的數(shù)據(jù)格式不符!"),
    SIGNATURE_NOT_MATCH("4001","請(qǐng)求的數(shù)字簽名不匹配!"),
    NOT_FOUND("4004", "未找到該資源!"),
    INTERNAL_SERVER_ERROR("5000", "服務(wù)器內(nèi)部錯(cuò)誤!"),
    SERVER_BUSY("5003","服務(wù)器正忙,請(qǐng)稍后再試!");

    /**
     * 錯(cuò)誤碼
     */
    private final String resultCode;

    /**
     * 錯(cuò)誤描述
     */
    private final String resultMsg;

    ExceptionEnum(String resultCode, String resultMsg) {
        this.resultCode = resultCode;
        this.resultMsg = resultMsg;
    }

    @Override
    public String getResultCode() {
        return resultCode;
    }

    @Override
    public String getResultMsg() {
        return resultMsg;
    }
}

2.3 自定義異常類

/**
 * @description: 自定義異常類
 * @author: DT
 * @date: 2021/4/19 21:44
 * @version: v1.0
 */
public class BizException extends RuntimeException{

    private static final long serialVersionUID = 1L;

    /**
     * 錯(cuò)誤碼
     */
    protected String errorCode;
    /**
     * 錯(cuò)誤信息
     */
    protected String errorMsg;

    public BizException() {
        super();
    }

    public BizException(BaseErrorInfoInterface errorInfoInterface) {
        super(errorInfoInterface.getResultCode());
        this.errorCode = errorInfoInterface.getResultCode();
        this.errorMsg = errorInfoInterface.getResultMsg();
    }

    public BizException(BaseErrorInfoInterface errorInfoInterface, Throwable cause) {
        super(errorInfoInterface.getResultCode(), cause);
        this.errorCode = errorInfoInterface.getResultCode();
        this.errorMsg = errorInfoInterface.getResultMsg();
    }

    public BizException(String errorMsg) {
        super(errorMsg);
        this.errorMsg = errorMsg;
    }

    public BizException(String errorCode, String errorMsg) {
        super(errorCode);
        this.errorCode = errorCode;
        this.errorMsg = errorMsg;
    }

    public BizException(String errorCode, String errorMsg, Throwable cause) {
        super(errorCode, cause);
        this.errorCode = errorCode;
        this.errorMsg = errorMsg;
    }


    public String getErrorCode() {
        return errorCode;
    }

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

    public String getErrorMsg() {
        return errorMsg;
    }

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

    @Override
    public Throwable fillInStackTrace() {
        return this;
    }
}

2.4 自定義數(shù)據(jù)傳輸

/**
 * @description: 自定義數(shù)據(jù)傳輸
 * @author: DT
 * @date: 2021/4/19 21:47
 * @version: v1.0
 */
public class ResultResponse {
    /**
     * 響應(yīng)代碼
     */
    private String code;

    /**
     * 響應(yīng)消息
     */
    private String message;

    /**
     * 響應(yīng)結(jié)果
     */
    private Object result;

    public ResultResponse() {
    }

    public ResultResponse(BaseErrorInfoInterface errorInfo) {
        this.code = errorInfo.getResultCode();
        this.message = errorInfo.getResultMsg();
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public Object getResult() {
        return result;
    }

    public void setResult(Object result) {
        this.result = result;
    }

    /**
     * 成功
     *
     * @return
     */
    public static ResultResponse success() {
        return success(null);
    }

    /**
     * 成功
     * @param data
     * @return
     */
    public static ResultResponse success(Object data) {
        ResultResponse rb = new ResultResponse();
        rb.setCode(ExceptionEnum.SUCCESS.getResultCode());
        rb.setMessage(ExceptionEnum.SUCCESS.getResultMsg());
        rb.setResult(data);
        return rb;
    }

    /**
     * 失敗
     */
    public static ResultResponse error(BaseErrorInfoInterface errorInfo) {
        ResultResponse rb = new ResultResponse();
        rb.setCode(errorInfo.getResultCode());
        rb.setMessage(errorInfo.getResultMsg());
        rb.setResult(null);
        return rb;
    }

    /**
     * 失敗
     */
    public static ResultResponse error(String code, String message) {
        ResultResponse rb = new ResultResponse();
        rb.setCode(code);
        rb.setMessage(message);
        rb.setResult(null);
        return rb;
    }

    /**
     * 失敗
     */
    public static ResultResponse error( String message) {
        ResultResponse rb = new ResultResponse();
        rb.setCode("-1");
        rb.setMessage(message);
        rb.setResult(null);
        return rb;
    }

    @Override
    public String toString() {
        return JSONObject.toJSONString(this);
    }

}

2.5 自定義全局異常處理

/**
 * @description: 自定義異常處理
 * @author: DT
 * @date: 2021/4/19 21:51
 * @version: v1.0
 */
@ControllerAdvice
public class GlobalExceptionHandler {
    
    private static final Logger logger = LoggerFactory.getLogger(GlobalExceptionHandler.class);

    /**
     * 處理自定義的業(yè)務(wù)異常
     * @param req
     * @param e
     * @return
     */
    @ExceptionHandler(value = BizException.class)
    @ResponseBody
    public ResultResponse bizExceptionHandler(HttpServletRequest req, BizException e){
        logger.error("發(fā)生業(yè)務(wù)異常!原因是:{}",e.getErrorMsg());
        return ResultResponse.error(e.getErrorCode(),e.getErrorMsg());
    }

    /**
     * 處理空指針的異常
     * @param req
     * @param e
     * @return
     */
    @ExceptionHandler(value =NullPointerException.class)
    @ResponseBody
    public ResultResponse exceptionHandler(HttpServletRequest req, NullPointerException e){
        logger.error("發(fā)生空指針異常!原因是:",e);
        return ResultResponse.error(ExceptionEnum.BODY_NOT_MATCH);
    }

    /**
     * 處理其他異常
     * @param req
     * @param e
     * @return
     */
    @ExceptionHandler(value =Exception.class)
    @ResponseBody
    public ResultResponse exceptionHandler(HttpServletRequest req, Exception e){
        logger.error("未知異常!原因是:",e);
        return ResultResponse.error(ExceptionEnum.INTERNAL_SERVER_ERROR);
    }
}

2.6 測(cè)試代碼

@PostMapping("/add")
public boolean add(@RequestBody User user) {
    //如果姓名為空就手動(dòng)拋出一個(gè)自定義的異常!
    if(user.getName()==null){
        throw  new BizException("-1","用戶姓名不能為空!");
    }
    return true;
}

如何在SpringBoot中實(shí)現(xiàn)全局異常處理方式

 @PutMapping("/update")
public boolean update(@RequestBody User user) {
    //這里故意造成一個(gè)空指針的異常,并且不進(jìn)行處理
    String str = null;
    str.equals("111");
    return true;
}

如何在SpringBoot中實(shí)現(xiàn)全局異常處理方式

 @DeleteMapping("/delete")
public boolean delete(@RequestBody User user)  {
    //這里故意造成一個(gè)異常,并且不進(jìn)行處理
    Integer.parseInt("abc123");
    return true;
}

如何在SpringBoot中實(shí)現(xiàn)全局異常處理方式

如果我們想捕獲這個(gè)類型轉(zhuǎn)換異常,是不是再添加一個(gè)遺產(chǎn)處理方法就可了。

如何在SpringBoot中實(shí)現(xiàn)全局異常處理方式

/**
* 處理類型轉(zhuǎn)換異常
 * @param req
 * @param e
 * @return
 */
@ExceptionHandler(value = NumberFormatException.class)
@ResponseBody
public ResultResponse exceptionHandler(HttpServletRequest req, NumberFormatException e){
    logger.error("發(fā)生類型轉(zhuǎn)換異常!原因是:",e);
    return ResultResponse.error(ExceptionEnum.PARAMS_NOT_CONVERT);
}
PARAMS_NOT_CONVERT("4002","類型轉(zhuǎn)換不對(duì)!"),

如何在SpringBoot中實(shí)現(xiàn)全局異常處理方式

自定義全局異常處理除了可以處理上述的數(shù)據(jù)格式之外,也可以處理頁(yè)面的跳轉(zhuǎn),只需在新增的異常方法的返回處理上填寫該跳轉(zhuǎn)的路徑并不使用ResponseBody 注解即可。

看完上述內(nèi)容是否對(duì)您有幫助呢?如果還想對(duì)相關(guān)知識(shí)有進(jìn)一步的了解或閱讀更多相關(guān)文章,請(qǐng)關(guān)注億速云行業(yè)資訊頻道,感謝您對(duì)億速云的支持。

向AI問(wèn)一下細(xì)節(jié)

免責(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)容。

AI