全局异常处理器-返回工具类-枚举

一、在类上面加上@RestControllerAdvice

在方法上添加的@Exceptionhandler(自定义异常类.class)

package com.jun.project.exception;

import com.jun.project.common.BaseResponse;
import com.jun.project.common.ErrorCode;
import com.jun.project.common.ResultUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 全局异常处理器
 *
 * @author jun
 */
@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    @ExceptionHandler(BusinessException.class)
    public BaseResponse<?> businessExceptionHandler(BusinessException e) {
        log.error("businessException: " + e.getMessage(), e);
        return ResultUtils.error(e.getCode(), e.getMessage());
    }

    @ExceptionHandler(RuntimeException.class)
    public BaseResponse<?> runtimeExceptionHandler(RuntimeException e) {
        log.error("runtimeException", e);
        return ResultUtils.error(ErrorCode.SYSTEM_ERROR, e.getMessage());
    }
}

二、设置自定义的异常类

继承运行时异常

/**
 * 自定义异常类
 *
 * @author jun
 */
public class BusinessException extends RuntimeException {

    private final int code;

    public BusinessException(int code, String message) {
        super(message);
        this.code = code;
    }

    public BusinessException(ErrorCode errorCode) {
        super(errorCode.getMessage());
        this.code = errorCode.getCode();
    }

    public BusinessException(ErrorCode errorCode, String message) {
        super(message);
        this.code = errorCode.getCode();
    }

    public int getCode() {
        return code;
    }
}

三、枚举类设置错误码

package com.jun.project.common;

/**
 * 错误码
 *
 * @author jun
 */
public enum ErrorCode {

    SUCCESS(0, "ok"),
    PARAMS_ERROR(40000, "请求参数错误"),
    NOT_LOGIN_ERROR(40100, "未登录"),
    NO_AUTH_ERROR(40101, "无权限"),
    NOT_FOUND_ERROR(40400, "请求数据不存在"),
    FORBIDDEN_ERROR(40300, "禁止访问"),
    SYSTEM_ERROR(50000, "系统内部异常"),
    OPERATION_ERROR(50001, "操作失败");

    /**
     * 状态码
     */
    private final int code;

    /**
     * 信息
     */
    private final String message;

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

    public int getCode() {
        return code;
    }

    public String getMessage() {
        return message;
    }

}

四、返回的工具类

package com.jun.project.common;

/**
 * 返回工具类
 *
 * @author jun
 */
public class ResultUtils {

    /**
     * 成功
     *
     * @param data
     * @param <T>
     * @return
     */
    public static <T> BaseResponse<T> success(T data) {
        return new BaseResponse<>(0, data, "ok");
    }

    /**
     * 失败
     *
     * @param errorCode
     * @return
     */
    public static BaseResponse error(ErrorCode errorCode) {
        return new BaseResponse<>(errorCode);
    }

    /**
     * 失败
     *
     * @param code
     * @param message
     * @return
     */
    public static BaseResponse error(int code, String message) {
        return new BaseResponse(code, null, message);
    }

    /**
     * 失败
     *
     * @param errorCode
     * @return
     */
    public static BaseResponse error(ErrorCode errorCode, String message) {
        return new BaseResponse(errorCode.getCode(), null, message);
    }
}

以上是开发中常使用的  项目中可以复用

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
全局异常是指在程序运行过程中可能出现的异常情况,而全局异常处理是一种机制,用于捕获和处理这些异常。全局异常处理可以统一处理项目中的所有异常,避免代码冗余和不通用的问题。\[1\] 在Java中,可以使用try...catch语句来直接捕获异常,但这种方式比较繁琐,特别是当有多个异常需要处理时。为了解决这个问题,可以使用全局异常处理来实现。在项目中定义一个通用的全局异常处理,通过注解@RestControllerAdvice来标识该类为全局异常处理。然后在处理中使用@ExceptionHandler注解来指定要处理的异常类型,并编写相应的处理逻辑。\[2\] 例如,在处理MySQL数据库异常时,可以在全局异常处理中定义一个方法,使用@ExceptionHandler(SQLException.class)注解来指定要处理的异常类型为SQLException。在方法中可以根据具体的异常类型进行不同的处理,例如判断是否为SQLIntegrityConstraintViolationException异常,如果是则返回相应的错误信息。\[2\] 除了使用全局异常处理,还可以使用其他方式来处理异常,例如在控制中使用@ExceptionHandler注解来处理局部异常,或者在配置文件中配置全局异常处理。根据具体的项目需求和开发规范,可以选择适合的方式来处理异常。\[1\] 总之,全局异常和全局异常处理Java中处理异常的一种机制,可以统一处理项目中的异常情况,提高代码的可维护性和可读性。通过定义一个通用的全局异常处理,可以避免代码冗余和不通用的问题,使异常处理更加简洁和高效。\[1\]\[2\] #### 引用[.reference_title] - *1* [JAVA全局异常处理](https://blog.csdn.net/weixin_53041251/article/details/126855725)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [Java全局异常处理设计](https://blog.csdn.net/weixin_44096133/article/details/123001210)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [自己编写Java全局异常处理](https://blog.csdn.net/weixin_34549315/article/details/114568477)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值