亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

SpringBoot實(shí)現(xiàn)全局異常的封裝和統(tǒng)一處理

 更新時(shí)間:2023年12月12日 08:10:31   作者:程序員無(wú)名  
在Spring Boot應(yīng)用中,全局異常的處理是一個(gè)非常重要的方面,本文主要為大家詳細(xì)介紹了如何在Spring Boot中進(jìn)行全局異常的封裝和統(tǒng)一處理,需要的可以參考下

前言

在Spring Boot應(yīng)用中,全局異常的處理是一個(gè)非常重要的方面,它可以提高系統(tǒng)的穩(wěn)定性和用戶體驗(yàn)。在這篇博客中,我們將介紹如何在Spring Boot中進(jìn)行全局異常的封裝和統(tǒng)一處理。

全局異常處理能夠捕獲應(yīng)用中所有未處理的異常,統(tǒng)一進(jìn)行處理,防止異常信息泄露到客戶端,同時(shí)也能夠記錄異常信息以便后續(xù)的調(diào)試和分析。

接下來(lái)就用SpringBoot實(shí)例代碼實(shí)現(xiàn)一個(gè)簡(jiǎn)單的全局異常攔截。

創(chuàng)建全局異常類

首先,我們需要?jiǎng)?chuàng)建一個(gè)自定義的全局異常類,繼承自RuntimeException,用于封裝業(yè)務(wù)異常信息。

/**
可直接進(jìn)行throw該異常類進(jìn)行返回信息
**/
public class CustomException extends RuntimeException {
    //錯(cuò)誤碼枚舉
    private final ErrorCode errorCode;

    public CustomException(ErrorCode errorCode) {
        super(errorCode.getMessage());
        this.errorCode = errorCode;
    }

    public ErrorCode getErrorCode() {
        return errorCode;
    }
}

創(chuàng)建錯(cuò)誤碼枚舉

為了更好地封裝異常信息,我們創(chuàng)建一個(gè)錯(cuò)誤碼的枚舉類,用于定義異常碼和異常信息。

public enum ErrorCode {
    SUCCESS("10000", "success"),
    SYSTEM_ERROR("500", "系統(tǒng)系統(tǒng),請(qǐng)聯(lián)系管理員"),
    NOT_FOUNT("404","未找到對(duì)應(yīng)的資源");

    // 其他錯(cuò)誤碼...

    private final String code;
    private final String message;

    ErrorCode(String code, String message) {
        this.code = code;
        this.message = message;
    }

    public String getCode() {
        return code;
    }

    public String getMessage() {
        return message;
    }
}

創(chuàng)建全局異常處理器

然后,我們創(chuàng)建一個(gè)全局異常處理器,使用@ControllerAdvice注解,配合@ExceptionHandler注解處理各種異常。

@ControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(CustomException.class)
    public ResponseEntity<Response> handleCustomException(CustomException ex) {
        ErrorCode errorCode = ex.getErrorCode();
        return new ResponseEntity<>(Response.error(errorCode.getCode(), errorCode.getMessage()), HttpStatus.INTERNAL_SERVER_ERROR);
    }

    @ExceptionHandler(Exception.class)
    public ResponseEntity<Response> handleOtherExceptions(Exception ex) {
        return new ResponseEntity<>(Response.error(), HttpStatus.INTERNAL_SERVER_ERROR);
    }
}

創(chuàng)建錯(cuò)誤響應(yīng)類

最后,我們創(chuàng)建一個(gè)錯(cuò)誤響應(yīng)類,用于返回統(tǒng)一的錯(cuò)誤格式。

@Data
public class Response<T> {
    private  String code;
    private  String message;
    private T data;
    //默認(rèn)的返回成功
    public static Response success(){
        Response response  =new Response();
        response.setCode(ErrorCode.SUCCESS.getCode());
        response.setMessage(ErrorCode.SUCCESS.getMessage());
        return response;
    }
    //帶內(nèi)容的返回成功
    public static <T> Response success(T data){
        Response<T> response  =new Response<T>();
        response.setCode(ErrorCode.SUCCESS.getCode());
        response.setData(data);
        response.setMessage(ErrorCode.SUCCESS.getMessage());
        return response;
    }
    //默認(rèn)的返回失敗
    public static Response error(){
        Response response  =new Response();
        response.setCode(ErrorCode.SYSTEM_ERROR.getCode());
        response.setMessage(ErrorCode.SYSTEM_ERROR.getMessage());
        return response;
    }
    //自定義的返回失敗
    public static Response error(String code,String msg){
        Response response  =new Response();
        response.setCode(code);
        response.setMessage(msg);
        return response;
    }
}

測(cè)試

現(xiàn)在,我們可以在業(yè)務(wù)代碼中通過(guò)拋出CustomException來(lái)觸發(fā)全局異常處理,例如:

@RestController
@RequestMapping("/api/test")
public class TestConroller {
    @GetMapping("/success")
    public ResponseEntity<Response> successObj(){
        return ResponseEntity.ok(Response.success("111111"));
    }
    @GetMapping("/error")
    public ResponseEntity<Response> error(String name) {
    //傳入的name等于張三時(shí),拋出我們的異常處理。
        if(StringUtils.equals(name,"zhangsan")){
            throw new CustomException(ErrorCode.SYSTEM_ERROR);
        }
        return ResponseEntity.ok(Response.success());
    }
}    

訪問(wèn):/api/test/success 返回:

{
"code": "10000",
"message": "success",
"data": "111111"
}

訪問(wèn):/api/test/error?name=zhangsan 返回:

{
"code": "500",
"message": "系統(tǒng)系統(tǒng),請(qǐng)聯(lián)系管理員",
"data": null
}

通過(guò)以上步驟,我們成功地封裝了全局異常并進(jìn)行了統(tǒng)一處理。這種方式不僅提高了代碼的可維護(hù)性,還使得異常信息的格式更加一致,方便客戶端或其他系統(tǒng)進(jìn)行處理。全局異常處理是一個(gè)非常值得關(guān)注的話題,尤其是在構(gòu)建健壯的后端應(yīng)用時(shí)。

到此這篇關(guān)于SpringBoot實(shí)現(xiàn)全局異常的封裝和統(tǒng)一處理的文章就介紹到這了,更多相關(guān)SpringBoot全局異常內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論