溫馨提示×

溫馨提示×

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

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

SpringBoot怎么進行統(tǒng)一異常處理

發(fā)布時間:2022-02-07 10:23:39 來源:億速云 閱讀:229 作者:iii 欄目:開發(fā)技術

這篇文章主要介紹“SpringBoot怎么進行統(tǒng)一異常處理”的相關知識,小編通過實際案例向大家展示操作過程,操作方法簡單快捷,實用性強,希望這篇“SpringBoot怎么進行統(tǒng)一異常處理”文章能幫助大家解決問題。

1、處理前

異常代碼

/**
     * 根據(jù)id獲取醫(yī)院設置
     *
     * @param id 查看的id編號
     * @return
     */
@ApiOperation(value = "根據(jù)id獲取醫(yī)院設置")
@GetMapping("/findHospById/{id}")
public Result findHospById(@PathVariable Long id) {
    // 模擬異常(因為除數(shù)不能為0)
    int a = 1 / 0;
    HospitalSet hospitalSet = hospitalSetService.getById(id);
    return Result.ok(hospitalSet);
}

Swagger2輸出結果

SpringBoot怎么進行統(tǒng)一異常處理

2、進行系統(tǒng)異常全局處理

添加全局異常處理類

SpringBoot怎么進行統(tǒng)一異常處理

代碼

package com.fafa.yygh.common.exception;

import com.fafa.yygh.common.result.Result;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 全局異常處理
 *
 * @author Sire
 * @version 1.0
 * @date 2022-02-02 21:01
 */
@ControllerAdvice
public class GlobalExceptionHandler {
    /**
     * 系統(tǒng)異常處理
     *
     * @param e
     * @return
     */
    @ExceptionHandler(Exception.class)
    @ResponseBody
    public Result error(Exception e) {
        e.printStackTrace();
        return Result.fail();
    }
}

Swagger2結果

SpringBoot怎么進行統(tǒng)一異常處理

3、進行自定義異常處理

開發(fā)時,往往需要我們去定義處理一些異常(這里還是那上面的那個異常來做測試)

創(chuàng)建自定義異常處理類

package com.fafa.yygh.common.exception;

import com.fafa.yygh.common.result.ResultCodeEnum;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

/**
 * 自定義全局異常類
 *
 * @author qy
 */
@Data
@ApiModel(value = "自定義全局異常類")
public class YyghException extends RuntimeException {

    @ApiModelProperty(value = "異常狀態(tài)碼")
    private Integer code;

    /**
     * 通過狀態(tài)碼和錯誤消息創(chuàng)建異常對象
     *
     * @param message
     * @param code
     */
    public YyghException(String message, Integer code) {
        super(message);
        this.code = code;
    }

    /**
     * 接收枚舉類型對象
     *
     * @param resultCodeEnum
     */
    public YyghException(ResultCodeEnum resultCodeEnum) {
        super(resultCodeEnum.getMessage());
        this.code = resultCodeEnum.getCode();
    }

    @Override
    public String toString() {
        return "YyghException{" +
            "code=" + code +
            ", message=" + this.getMessage() +
            '}';
    }
}

將其添加到GlobalExceptionHandler

/**
     * 自定義異常處理
     *
     * @param e
     * @return
     */
@ExceptionHandler(YyghException.class)
@ResponseBody
public Result divError(YyghException e) {
    return Result.build(e.getCode(), e.getMessage());
}

SpringBoot怎么進行統(tǒng)一異常處理

需要手動 try catch 一下

SpringBoot怎么進行統(tǒng)一異常處理

效果

swagger和系統(tǒng)異常處理一樣

不過后臺輸出不一樣

SpringBoot怎么進行統(tǒng)一異常處理

關于“SpringBoot怎么進行統(tǒng)一異常處理”的內容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關的知識,可以關注億速云行業(yè)資訊頻道,小編每天都會為大家更新不同的知識點。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內容。

AI