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

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

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

1 定義錯誤碼類

? 可以定義各種錯誤碼枚舉,比如業(yè)務,系統(tǒng)相關的報錯信息

/**
 * 錯誤代碼
 * 錯誤碼
 *
 * @author leovany
 * @date 2023/09/23
 */
public enum ErrorCode {
    SUCCESS(0, "success", ""),
    ERROR_PARAMS(40000, "請求參數(shù)錯誤", ""),
    ERROR_NULL(40001, "請求數(shù)據(jù)為空", ""),
    ERROR_LOGIN(40100, "未登錄", ""),
    ERROR_NO_AUTH(41001, "無權限", ""),
    ERROR_SYSTEM(50000, "系統(tǒng)內部異常", "")
    ;
    /**
     * 錯誤碼ID
     */
    private final int code;
    /**
     * 錯誤碼信息
     */
    private final String message;
    /**
     * 錯誤碼描述(詳情)
     */
    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è)務異常類

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

    擴展了 codedescription兩個字段

  • 自定義構造函數(shù),更靈活 / 快捷的設置字段
import com.leovany.usercenter.common.ErrorCode;
/**
 * 業(yè)務異常
 * 自定義業(yè)務異常類
 *
 * @author leovany
 * @date 2023/09/23
 */
public class BusinessException extends RuntimeException {
    /**
     * 錯誤碼
     */
    private final int code;
    /**
     * 描述
     */
    private final String description;
    /**
     * 業(yè)務異常
     *
     * @param message     信息
     * @param code        錯誤碼
     * @param description 描述
     */
    public BusinessException(String message, int code, String description) {
        super(message);
        this.code = code;
        this.description = description;
    }
    /**
     * 業(yè)務異常
     *
     * @param errorCode 錯誤代碼
     */
    public BusinessException(ErrorCode errorCode) {
        super(errorCode.getMessage());
        this.code = errorCode.getCode();
        this.description = errorCode.getDescription();
    }
    /**
     * 業(yè)務異常
     *
     * @param errorCode   錯誤代碼
     * @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實現(xiàn),在調用方法前后進行額外的處理

作用

  • 捕獲代碼中所有的異常,讓前端得到更詳細的業(yè)務報錯信息
  • 屏蔽掉項目框架本身的異常,不暴露服務器的內部狀態(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);
}
  • 代碼對比

5 前端請求效果

總結

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

以上就是java封裝全局異常處理深入詳解的詳細內容,更多關于java封裝全局異常處理的資料請關注腳本之家其它相關文章!

相關文章

最新評論

建宁县| 明星| 交口县| 板桥市| 宁德市| 盐山县| 土默特右旗| 固镇县| 穆棱市| 瓮安县| 额尔古纳市| 阿尔山市| 静乐县| 赤水市| 静海县| 衡东县| 托克托县| 大方县| 正安县| 镇远县| 赞皇县| 北宁市| 和硕县| 吉林市| 醴陵市| 贡嘎县| 霸州市| 红原县| 安溪县| 安国市| 绥宁县| 来安县| 通化县| 芒康县| 安岳县| 库伦旗| 曲松县| 怀宁县| 那坡县| 遂川县| 盖州市|