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

SpringMVC異常全局捕獲與錯誤響應的處理方法

 更新時間:2025年03月18日 09:35:41   作者:小安同學iter  
編程式異常處理是通過在代碼中?顯式編寫異常捕獲邏輯(如?try-catch?塊)來管理異常的方式,開發(fā)者需要手動處理每一個可能拋出異常的代碼段,本文給大家介紹SpringMVC異常全局捕獲與錯誤響應的處理方法,感興趣的朋友一起看看吧

一編程式異常處理

編程式異常處理是通過在代碼中 顯式編寫異常捕獲邏輯(如 try-catch 塊)來管理異常的方式。開發(fā)者需要手動處理每一個可能拋出異常的代碼段。

代碼實現(xiàn):

package org.example.springmvc.controller;
import org.example.springmvc.common.R;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class helloController {
    @GetMapping(value = "/hello")
    public R hello() {
        // 編程式的異常處理
        try {
            int i = 1 / 0;
            return R.ok(i);
        } catch (Exception e) {
            return R.error(100, "執(zhí)行異常");
        }
    }
}

運行結果:

二聲明式異常處理

聲明式異常處理是通過 配置或注解 將異常處理邏輯與業(yè)務代碼解耦,通常由框架統(tǒng)一管理。例如,在 Spring 中通過 @ControllerAdvice 和 @ExceptionHandler 實現(xiàn)全局異常處理。

代碼實現(xiàn):

package org.example.springmvc.controller;
import org.example.springmvc.common.R;
import org.springframework.web.bind.annotation.*;
@RestController
public class HelloController {
    @GetMapping(value = "/hello")
    public R hello(@RequestParam(value = "i", defaultValue = "0") Integer i) {
        int j = 1 / i;
        return R.ok(j);
    }
    /**
     * 如果Controller本類出現(xiàn)異常,會自動在本類中尋找有沒有@ExceptionHandler注解的方法,如果有,則執(zhí)行
     * 可以根據(jù)需求添加多個@ExceptionHandler注解的方法
     * .@ExceptionHandler注解接收多個參數(shù),表示可以攔截多個異常類型
     *
     */
    @ExceptionHandler(ArithmeticException.class)
    public R handleArithmeticException(ArithmeticException e) {
        return R.error(100, "是執(zhí)行異常" + e.getMessage());
    }
    @ExceptionHandler(Exception.class)
    public R handleException(Exception e) {
        return R.error(200, "異常" + e.getMessage());
    }
}

改進:

單一的@ExceptionHandler注解只會處理當前類 @ExceptionHandler+@ControllerAdvice注解可以完成全局統(tǒng)一處理

處理優(yōu)先級:本類大于外類,精確大于全局

SpringBoot底層對SpringMVC有兜底處理機制(沒有寫這個異常處理邏輯)

package org.example.springmvc.advice;
import org.example.springmvc.common.R;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
//@ResponseBody
//@ControllerAdvice
@RestControllerAdvice//合成注解
public class GlobalExceptionHandler {
    @ExceptionHandler(ArithmeticException.class)
    public R handleArithmeticException(ArithmeticException e) {
        return R.error(100, "是執(zhí)行異常" + e.getMessage());
    }
    @ExceptionHandler(Exception.class)
    public R handleException(Exception e) {
        return R.error(200, "異常" + e.getMessage());
    }
}

運行結果:

三 實際場景(拓展)

問題場景:

Controller層業(yè)務邏輯感知異常,被全局異常處理器捕獲,然后調(diào)用異常對象的構造方法,使用業(yè)務枚舉類將異常碼和異常原因傳遞給異常對象,最終被全局處理器解決

業(yè)務拋出:

throw new BizException(BizExceptionEnum.ORDER_NOT_EXIST);

1 業(yè)務枚舉類BizExceptionEnum 將所有可能出現(xiàn)的code與msg枚舉封裝

package org.example.springmvc.exception;
public enum BizExceptionEnum {
    //ORDER訂單模塊
    ORDER_NOT_EXIST(10001, "訂單不存在"),
    ORDER_STATUS_ERROR(10002, "訂單狀態(tài)錯誤"),
    ORDER_PAID(10003, "訂單已支付"),
    ORDER_CANCEL_FAIL(10004, "訂單取消失敗"),
    ORDER_PAY_FAIL(10005, "訂單支付失敗"),
    //PRODUCT商品模塊
    PRODUCT_STOCK_NOT_ENOUGH(20001, "商品庫存不足"),
    PRODUCT_OFF_SALE_OR_DELETE(20002, "商品下架或刪除"),
    PRODUCT_NOT_EXIST(20003, "商品不存在"),
    PRODUCT_STOCK_ERROR(20004, "商品庫存有誤"),
    //USER用戶模塊
    USER_NOT_EXIST(30001, "用戶不存在"),
    USER_PASSWORD_ERROR(30002, "密碼錯誤"),
    USER_ACCOUNT_ERROR(30003, "賬號錯誤"),
    USER_ACCOUNT_LOCK(30004, "賬號已被鎖定"),
    USER_LOGIN_FAIL(30005, "用戶名或密碼錯誤"),
    USER_ACCOUNT_FORBIDDEN(30006, "賬號已被禁用"),
    //OTHER其他模塊
    OTHER_ERROR(40001, "其他錯誤"),
    ;
    private final int code;
    private final String msg;
    BizExceptionEnum(int code, String msg) {
        this.code = code;
        this.msg = msg;
    }
    public int getCode() {
        return code;
    }
    public String getMsg() {
        return msg;
    }
}

2 業(yè)務異常類(BizException)在這個方法當中接收BizExceptionEnum封裝的信息,使用構造方法接收。

package org.example.springmvc.exception;
public class BizException extends RuntimeException{
    //業(yè)務異常碼,業(yè)務異常原因
    private final Integer code;
    private final String msg;
    public BizException(Integer code, String msg) {
        super(msg);
        this.code = code;
        this.msg = msg;
    }
    //Controller感知異常,被全局異常處理器捕獲,然后調(diào)用異常對象的構造方法,將異常碼和異常原因傳遞給異常對象,
    //通過這個構造方法,可以將BizExceptionEnum枚舉對象 傳給 BizException,然后拋出異常
    public BizException(BizExceptionEnum bizExceptionEnum) {
        super(bizExceptionEnum.getMsg());
        this.code = bizExceptionEnum.getCode();
        this.msg = bizExceptionEnum.getMsg();
    }
    public Integer getCode() {
        return code;
    }
    public String getMsg() {
        return msg;
    }
}

3 全局異常處理器GlobalExceptionHandle:最終在controller層處理業(yè)務邏輯的時候controller調(diào)用的是全局異常處理器的方法。

package org.example.springmvc.advice;
import org.example.springmvc.common.R;
import org.example.springmvc.exception.BizException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
//@ResponseBody
//@ControllerAdvice
@RestControllerAdvice//合成注解
public class GlobalExceptionHandler {
    @ExceptionHandler(ArithmeticException.class)
    public R handleArithmeticException(ArithmeticException e) {
        return R.error(100, "是執(zhí)行異常" + e.getMessage());
    }
    @ExceptionHandler(BizException.class)//這里傳遞的e已經(jīng)提前封裝好了
    public R HandleBizException(BizException e) {
        return R.error(e.getCode(), e.getMsg());
    }
    @ExceptionHandler(Throwable.class)
    public R handleThrowable(Throwable e) {
        return R.error(500, "異常" + e.getMessage());
    }
}

到此這篇關于SpringMVC異常全局捕獲與錯誤響應的處理方法的文章就介紹到這了,更多相關SpringMVC異常全局捕獲內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

最新評論