错误接口

/**
 * @Author LiGuangLong
 * @Date 2021-10-14 13:20
 * @Version 1.0
 **/
public interface BaseErrorInfoInterface {
    /**
     * 获取错误码
     * @return str
     */
    int getResultCode();

    /**
     * 错误描述
     * @return str
     */
    String getResultMsg();
}

返回数据状态类


public enum ErrorEnum implements BaseErrorInfoInterface{
    //返回数据 状态码 信息 定义
    SUCCESS(200, "成功"),
    BODY_NOT_MATCH(400,"请求的数据格式不符!"),
    TOKEN_EXPIRE(401, "授权过期"),
    TOKEN_NOT_ENOUGH(402, "权限不足"),
    REQUEST_ENCRYPT_ERROR(403, "请求参数,加密内容未解密成功"),
    REQUEST_METHOD_ERROR(404, "请求方法错误"),
    PARAM_LACK(501, "必填参数缺失"),
    PARAM_ERROR(502, "请求参数格式非法,或存在非法参数"),
    RES_NOT_FIND(503, "没找到对应的数据"),
    LOGIN_FAIL(504, "登录失败,用户名或密码错误"),
    APP_VERSION_LOW(505, "应用版本过低,请尽快升级!"),
    BUSINESS_ERROR(500, "业务处理失败"),
    ;

    ErrorEnum(int errorCode, String errorMsg){
        this.errorCode = errorCode;
        this.errorMsg = errorMsg;
    }

    private int errorCode;
    private String errorMsg;

    public int getErrorCode() {
        return errorCode;
    }

    public String getErrorMsg() {
        return errorMsg;
    }


    @Override
    public int getResultCode() {
        return this.errorCode;
    }

    @Override
    public String getResultMsg() {
        return this.errorMsg;
    }
}

异常捕获类


/**
 * @Author LiGuangLong
 * @Date 2021-10-14 13:57
 * @Version 1.0
 **/
public class BizException extends RuntimeException{
    private static final long serialVersionUID = 1L;

    /**
     * 错误码
     */
    protected int errorCode;
    /**
     * 错误信息
     */
    protected String errorMsg;

    public BizException() {
        super();
    }

    public BizException(BaseErrorInfoInterface errorInfoInterface) {
        super(errorInfoInterface.getResultMsg());
        this.errorCode = errorInfoInterface.getResultCode();
        this.errorMsg = errorInfoInterface.getResultMsg();
    }

    public BizException(BaseErrorInfoInterface errorInfoInterface, Throwable cause) {
        super(errorInfoInterface.getResultMsg(), cause);
        this.errorCode = errorInfoInterface.getResultCode();
        this.errorMsg = errorInfoInterface.getResultMsg();
    }

    public BizException(String errorMsg) {
        super(errorMsg);
        this.errorMsg = errorMsg;
    }

    public BizException(int errorCode, String errorMsg) {
        super(errorMsg);
        this.errorCode = errorCode;
        this.errorMsg = errorMsg;
    }

    public BizException(int errorCode, String errorMsg, Throwable cause) {
        super(errorMsg, cause);
        this.errorCode = errorCode;
        this.errorMsg = errorMsg;
    }

    /**
     * 父类不存在 code
     * @return code
     */
    public int getErrorCode() {
        return errorCode;
    }
    public void setErrorCode(int errorCode) {
        this.errorCode = errorCode;
    }
    public void setErrorMsg(String errorMsg) {
        this.errorMsg = errorMsg;
    }

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


    @Override
    public Throwable fillInStackTrace() {
        return this;
    }
}

自定义异常类


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * @Author LiGuangLong
 * @Date 2021-10-14 13:21
 * @Version 1.0
 **/
@ControllerAdvice
public class GlobalExceptionHandler {
    private static final Logger logger = LoggerFactory.getLogger(GlobalExceptionHandler.class);

    /**
     * 处理自定义的业务异常
     * @param req req
     * @param e e
     */
    @ExceptionHandler(value = BizException.class)
    @ResponseBody
    public  ResponseDTO bizExceptionHandler(HttpServletRequest req, BizException e){
        logger.error("发生业务异常!原因是:{}",e.getMessage());
        return new ResponseDTO(e.getErrorCode(),e.getMessage());
    }

    /**
     * 处理空指针的异常
     * @param req req
     * @param e e
     */
    @ExceptionHandler(value =NullPointerException.class)
    @ResponseBody
    public ResponseDTO exceptionHandler(HttpServletRequest req, NullPointerException e){
        logger.error("发生空指针异常!原因是:",e);
        return new ResponseDTO(ErrorEnum.BODY_NOT_MATCH.getResultCode(),ErrorEnum.BODY_NOT_MATCH.getResultMsg());
    }

    /**
     *
     * @param e 异常类型
     * @return 异常result
     */
    @ExceptionHandler(value = ArithmeticException.class)
    @ResponseBody
    public ResponseDTO exceptionHandler(ArithmeticException e){
        logger.error("除数不可以为0异常! 原因",e);
        return new ResponseDTO(ErrorEnum.BODY_NOT_MATCH.getResultCode(),ErrorEnum.BODY_NOT_MATCH.getErrorMsg());
    }


    /**
     * 处理其他异常
     * @param req
     * @param e
     */
    @ExceptionHandler(value =Exception.class)
    @ResponseBody
    public ResponseDTO exceptionHandler(HttpServletRequest req, Exception e){
        logger.error("未知异常!原因是:",e);
        return new ResponseDTO(ErrorEnum.BUSINESS_ERROR.getResultCode(),ErrorEnum.BUSINESS_ERROR.getResultMsg());
    }



}

返回信息模板


import com.fasterxml.jackson.annotation.JsonInclude;

@JsonInclude(JsonInclude.Include.NON_NULL)
public class ResponseDTO<T> {

    //private String status;
    private int code;
    private String message;
    private Long total;
    private T data;

    public ResponseDTO(T data) {
        this.data = data;
        this.code = 200;
        this.message = "成功";
        //this.status = "SUCCESS";
    }
    public ResponseDTO(T data,Long total) {
        this.total = total;
        this.data = data;
        this.code = 200;
        this.message = "成功";
        //this.status = "SUCCESS";
    }

    public ResponseDTO(ErrorEnum errorEnum){
        //this.status = errorEnum.name();
        this.code = errorEnum.getErrorCode();
        this.message = errorEnum.getErrorMsg();
    }
    public ResponseDTO(int code, String message){
        //this.status = "";
        this.code = code;
        this.message = message;
    }
    public ResponseDTO(ErrorEnum errorEnum, String message){
        //this.status = errorEnum.name();
        this.code = errorEnum.getErrorCode();
        this.message = message;
    }

    public ResponseDTO(String status, int code, String message) {
        //this.status = status;
        this.code = code;
        this.message = message;
    }

//    public String getStatus() {
//        return status;
//    }

    //public void setStatus(String status) {
//        this.status = status;
//    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }
    public Long getTotal(){
        return total;
    }
    public void setTotal(Long total){ this.total = total; }
}

controller 再也不用try catch了
spring boot 全局捕获异常_ide