最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

java封裝全局異常處理深入詳解

 更新時(shí)間:2023年09月24日 11:31:40   作者:Leovany  
這篇文章主要為大家介紹了java封裝全局異常處理的深入詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪

1 定義錯(cuò)誤碼類

? 可以定義各種錯(cuò)誤碼枚舉,比如業(yè)務(wù),系統(tǒng)相關(guān)的報(bào)錯(cuò)信息

/**
 * 錯(cuò)誤代碼
 * 錯(cuò)誤碼
 *
 * @author leovany
 * @date 2023/09/23
 */
public enum ErrorCode {
    SUCCESS(0, "success", ""),
    ERROR_PARAMS(40000, "請(qǐng)求參數(shù)錯(cuò)誤", ""),
    ERROR_NULL(40001, "請(qǐng)求數(shù)據(jù)為空", ""),
    ERROR_LOGIN(40100, "未登錄", ""),
    ERROR_NO_AUTH(41001, "無(wú)權(quán)限", ""),
    ERROR_SYSTEM(50000, "系統(tǒng)內(nèi)部異常", "")
    ;
    /**
     * 錯(cuò)誤碼ID
     */
    private final int code;
    /**
     * 錯(cuò)誤碼信息
     */
    private final String message;
    /**
     * 錯(cuò)誤碼描述(詳情)
     */
    private final String description;
    ErrorCode(int code, String message, String description) {
        this.code = code;
        this.message = message;
        this.description = description;
    }
    public int getCode() {
        return code;
    }
    public String getMessage() {
        return message;
    }
    public String getDescription() {
        return description;
    }
}

2 定義業(yè)務(wù)異常類

  • 相對(duì)于 java 的異常類,支持更多字段

    擴(kuò)展了 codedescription兩個(gè)字段

  • 自定義構(gòu)造函數(shù),更靈活 / 快捷的設(shè)置字段
import com.leovany.usercenter.common.ErrorCode;
/**
 * 業(yè)務(wù)異常
 * 自定義業(yè)務(wù)異常類
 *
 * @author leovany
 * @date 2023/09/23
 */
public class BusinessException extends RuntimeException {
    /**
     * 錯(cuò)誤碼
     */
    private final int code;
    /**
     * 描述
     */
    private final String description;
    /**
     * 業(yè)務(wù)異常
     *
     * @param message     信息
     * @param code        錯(cuò)誤碼
     * @param description 描述
     */
    public BusinessException(String message, int code, String description) {
        super(message);
        this.code = code;
        this.description = description;
    }
    /**
     * 業(yè)務(wù)異常
     *
     * @param errorCode 錯(cuò)誤代碼
     */
    public BusinessException(ErrorCode errorCode) {
        super(errorCode.getMessage());
        this.code = errorCode.getCode();
        this.description = errorCode.getDescription();
    }
    /**
     * 業(yè)務(wù)異常
     *
     * @param errorCode   錯(cuò)誤代碼
     * @param description 描述
     */
    public BusinessException(ErrorCode errorCode, String description) {
        super(errorCode.getMessage());
        this.code = errorCode.getCode();
        this.description = description;
    }
    public int getCode() {
        return code;
    }
    public String getDescription() {
        return description;
    }
}

3 全局異常處理器

通過Spring AOP實(shí)現(xiàn),在調(diào)用方法前后進(jìn)行額外的處理

作用

  • 捕獲代碼中所有的異常,讓前端得到更詳細(xì)的業(yè)務(wù)報(bào)錯(cuò)信息
  • 屏蔽掉項(xiàng)目框架本身的異常,不暴露服務(wù)器的內(nèi)部狀態(tài)
  • 集中處理,比如還可以做記錄日志
import com.leovany.usercenter.common.ResultVO;
import com.leovany.usercenter.common.ErrorCode;
import com.leovany.usercenter.common.ResultUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
/**
 * 全局異常處理類
 */
@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {
    /**
     * 處理異常-BusinessException
     * @param e
     * @return
     */
    @ExceptionHandler(BusinessException.class)
    public ResultVO<?> businessExceptionHandler(BusinessException e){
        log.error("businessException:" + e.getMessage(),e);
        return ResultUtils.error(e.getCode(),e.getMessage(),e.getDescription());
    }
    /**
     * 處理異常-RuntimeException
     * @param e
     * @return
     */
    @ExceptionHandler(RuntimeException.class)
    public ResultVO<?> runtimeExceptionHandler(RuntimeException e){
        log.error("runtimeException:" + e);
        return ResultUtils.error(ErrorCode.ERROR_SYSTEM,e.getMessage());
    }
}

4 使用

throw new BusinessException可以在方法中,任意地方拋出,很方便

  • 示例代碼
@PostMapping("/login")
public ResultVO<User> userLogin(@RequestBody UserLoginRequest userLoginRequest, HttpServletRequest request) {
    String userAccount = userLoginRequest.getUserAccount();
    String userPassword = userLoginRequest.getUserPassword();
    if (StringUtils.isAnyBlank(userAccount, userPassword)) {
        throw new BusinessException(ErrorCode.ERROR_PARAMS);
    }
    User user = userService.doLogin(userAccount, userPassword, request);
    return ResultUtils.success(user);
}
  • 代碼對(duì)比

5 前端請(qǐng)求效果

總結(jié)

通過封裝全局異常處理,對(duì)異常信息做了統(tǒng)一處理,讓前端得到更詳細(xì)的業(yè)務(wù)信息,同時(shí)保證系統(tǒng)的安全性(不會(huì)暴露系統(tǒng)內(nèi)部信息),在代碼上對(duì)參數(shù)校驗(yàn)等方面提供更加方便的形式。

以上就是java封裝全局異常處理深入詳解的詳細(xì)內(nèi)容,更多關(guān)于java封裝全局異常處理的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評(píng)論

濮阳县| 康平县| 泰和县| 兴化市| 锡林郭勒盟| 阜新| 昌图县| 平舆县| 仪征市| 唐山市| 黔西| 松阳县| 黑水县| 石屏县| 通道| 犍为县| 江城| 阳西县| 碌曲县| 雷山县| 徐州市| 海林市| 思南县| 门头沟区| 定远县| 南川市| 皋兰县| 乌拉特后旗| 上栗县| 大姚县| 获嘉县| 凭祥市| 探索| 宁远县| 喜德县| 兴山县| 阳新县| 陵川县| 双鸭山市| 中牟县| 德兴市|