Springboot @RestControllerAdvice全局异常捕获处理

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档

文章目录


一、@RestControllerAdvice

@RestControllerAdvice是Spring框架提供的一个注解,用于定义全局异常处理器和全局数据绑定设置。它结合了@ControllerAdvice和@ResponseBody两个注解的功能。
通过在@RestControllerAdvice类中定义异常处理方法,我们可以捕获和处理控制器中抛出的异常,提供自定义的异常处理逻辑,以及返回适当的响应给客户端

二、案例

1.自定义异常

java 复制代码
package com.easyblog.exception;


import com.easyblog.entity.enums.ResponseCodeEnum;

public class BusinessException extends RuntimeException {

    private ResponseCodeEnum codeEnum;

    private Integer code;

    private String message;

    public BusinessException(String message, Throwable e) {
        super(message, e);
        this.message = message;
    }

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

    public BusinessException(Throwable e) {
        super(e);
    }

    public BusinessException(ResponseCodeEnum codeEnum) {
        super(codeEnum.getMsg());
        this.codeEnum = codeEnum;
        this.code = codeEnum.getCode();
        this.message = codeEnum.getMsg();
    }

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

    public ResponseCodeEnum getCodeEnum() {
        return codeEnum;
    }

    public Integer getCode() {
        return code;
    }

    @Override
    public String getMessage() {
        return message;
    }

    /**
     * 重写fillInStackTrace 业务异常不需要堆栈信息,提高效率.
     */
    @Override
    public Throwable fillInStackTrace() {
        return this;
    }

}

2.自定义相应常量

java 复制代码
package com.easyblog.entity.enums;

public enum ResponseCodeEnum {
    CODE_200(200, "请求成功"),
    CODE_404(404, "请求地址不存在"),
    CODE_600(600, "请求参数错误"),
    CODE_601(601, "信息已经存在"),
    CODE_602(602, "信息提交过多"),
    CODE_500(500, "服务器返回错误,请联系管理员"),
    CODE_900(900, "http请求超时"),
    CODE_901(901, "登录超时,请重新登录");

    private Integer code;

    private String msg;

    ResponseCodeEnum(Integer code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public Integer getCode() {
        return code;
    }

    public String getMsg() {
        return msg;
    }
}

3.自定义响应类

java 复制代码
package com.easyblog.result;

import lombok.Data;

/**
 * 全局统一返回结果类
 */
@Data

public class Result<T> {
    private Integer code;
    private String message;

    private T data;

    public Result(){}

    protected static <T> Result<T> build(T data) {
        Result<T> result = new Result<T>();
        if (data != null)
            result.setData(data);
        return result;
    }

    public static <T> Result<T> build(T body, ResultCodeEnum resultCodeEnum) {
        Result<T> result = build(body);
        result.setCode(resultCodeEnum.getCode());
        result.setMessage(resultCodeEnum.getMessage());
        return result;
    }

    public static <T> Result<T> build(Integer code, String message) {
        Result<T> result = build(null);
        result.setCode(code);
        result.setMessage(message);
        return result;
    }

    public static<T> Result<T> ok(){
        return Result.ok(null);
    }

    /**
     * 操作成功
     * @param data
     * @param <T>
     * @return
     */
    public static<T> Result<T> ok(T data){
        Result<T> result = build(data);
        return build(data, ResultCodeEnum.SUCCESS);
    }

    public static<T> Result<T> fail(){
        return Result.fail(null);
    }

    /**
     * 操作失败
     * @param data
     * @param <T>
     * @return
     */
    public static<T> Result<T> fail(T data){
        Result<T> result = build(data);
        return build(data, ResultCodeEnum.FAIL);
    }

    public Result<T> message(String msg){
        this.setMessage(msg);
        return this;
    }

    public Result<T> code(Integer code){
        this.setCode(code);
        return this;
    }

    public boolean isOk() {
        if(this.getCode().intValue() == ResultCodeEnum.SUCCESS.getCode().intValue()) {
            return true;
        }
        return false;
    }
}

4.核心逻辑

java 复制代码
package com.easyblog.exception;
import com.easyblog.entity.enums.ResponseCodeEnum;
import com.easyblog.result.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.validation.BindException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.NoHandlerFoundException;

import javax.servlet.http.HttpServletRequest;
/**
 * 全局异常捕获
 * 可以捕获和处理控制器中抛出的异常,提供自定义的异常处理逻辑,以及返回适当的响应给客户端。
 * **/
@RestControllerAdvice
public class GlobalExceptionHandlerController {

    @ExceptionHandler(value = Exception.class)
    Object handleException(Exception e, HttpServletRequest request) {
        Result response=new Result<>();
        //404
        if (e instanceof NoHandlerFoundException) {
            response.setCode(ResponseCodeEnum.CODE_404.getCode());
            response.setMessage(ResponseCodeEnum.CODE_404.getMsg());
        } else if (e instanceof BusinessException) {
            //业务错误
            BusinessException biz = (BusinessException) e;
            response.setCode(biz.getCode());
            if (biz.getCode() == null) {
                response.setCode(ResponseCodeEnum.CODE_600.getCode());
            }
            response.setMessage(biz.getMessage());
        } else if (e instanceof BindException) {
            //参数类型错误
            response.setCode(ResponseCodeEnum.CODE_600.getCode());
            response.setMessage(ResponseCodeEnum.CODE_600.getMsg());
        } else if (e instanceof DuplicateKeyException) {
            //主键冲突
            response.setCode(ResponseCodeEnum.CODE_601.getCode());
            response.setMessage(ResponseCodeEnum.CODE_601.getMsg());
        } else {
            response.setCode(ResponseCodeEnum.CODE_500.getCode());
            response.setMessage(ResponseCodeEnum.CODE_500.getMsg());
        }
        return response;
    }
}

其中@ExceptionHandler:用于指定异常处理方法。当与@RestControllerAdvice配合使用时,用于全局处理控制器里的异常。

5.测试


相关推荐
2401_8543910813 分钟前
城镇住房保障:SpringBoot系统功能概览
java·spring boot·后端
hummhumm15 分钟前
Oracle 第29章:Oracle数据库未来展望
java·开发语言·数据库·python·sql·oracle·database
陈随易18 分钟前
兔小巢收费引发的论坛调研Node和Deno有感
前端·后端·程序员
聪明的墨菲特i23 分钟前
Django前后端分离基本流程
后端·python·django·web3
wainyz24 分钟前
Java NIO操作
java·开发语言·nio
工业3D_大熊29 分钟前
【虚拟仿真】CEETRON SDK在船舶流体与结构仿真中的应用解读
java·python·科技·信息可视化·c#·制造·虚拟现实
lzb_kkk38 分钟前
【JavaEE】JUC的常见类
java·开发语言·java-ee
爬山算法1 小时前
Maven(28)如何使用Maven进行依赖解析?
java·maven
hlsd#1 小时前
go mod 依赖管理
开发语言·后端·golang
陈大爷(有低保)1 小时前
三层架构和MVC以及它们的融合
后端·mvc