乐趣区

Spring-Boot-如何处理全局异常

1. 定义接口统一返回标准

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Getter;
import lombok.Setter;
import java.io.Serializable;
@ApiModel(value = "统一返回数据格式")
@Getter
@Setter
public class ResultMsg implements Serializable {@ApiModelProperty(value = "状态码")
    private int code;
    @ApiModelProperty(value = "返回结果描述")
    private String msg;
    @ApiModelProperty(value = "返回结果")
    private Object data;
    public ResultMsg(ResultMsgStatus resultMsgStatus) {this.code = resultMsgStatus.getCode();
        this.msg = resultMsgStatus.getMsg();}
    public ResultMsg(ResultMsgStatus resultMsgStatus, Object data) {this.code = resultMsgStatus.getCode();
        this.msg = resultMsgStatus.getMsg();
        this.data = data;
    }
    private ResultMsg setMsg(String msg) {
        this.msg = msg;
        return this;
    }
    public static ResultMsg success(Object data) {return new ResultMsg(ResultMsgStatus.SUCCESS, data);
    }
    public static ResultMsg success() {return new ResultMsg(ResultMsgStatus.SUCCESS, "");
    }
    public static ResultMsg failure(String failMsg) {return new ResultMsg(ResultMsgStatus.FAILURE).setMsg(failMsg);
    }
}

2. 定义错误码

import lombok.Getter;
@Getter
public enum ResultMsgStatus {
    /**
     * 请求失败
     */
    FAILURE(0, "请求失败"),
    /**
     * 请求成功
     */
    SUCCESS(200, "成功"),
    /**
     * Token 无效
     */
    TOKEN_INVALID(401, "Token 无效"),
    /**
     * 服务器错误
     */
    SERVER_ERROR(500, "服务器错误");
    private int code;
    private String msg;
    private ResultMsgStatus(int code, String msg) {
        this.code = code;
        this.msg = msg;
    }
}

3. 定义异常基类

import com.bubble.salt.msg.ResultMsgStatus;
/**
 * 异常基类,所有自定义的异常类均需要派生自该类
 */
public class BaseException extends RuntimeException{
    /**
     * 具体异常码
     */
    private int code=0;
    public BaseException(ResultMsgStatus status, Throwable cause) {super(status.getMsg(), cause);
        this.code=status.getCode();}
    public BaseException(ResultMsgStatus status) {super(status.getMsg());
        this.code=code;
    }
    public BaseException(String msg){super(msg);
        this.code = ResultMsgStatus.FAILURE.getCode();}
    public BaseException(ResultMsgStatus status, String msg){super(msg);
        this.code = status.getCode();}
    public int getCode() {return code;}
    protected void setCode(int code){this.code=code;}
}

4. 自定义异常

import com.bubble.salt.msg.ResultMsgStatus;
/**
 * @BelongsPackage: com.bubble.salt.exception
 * @CreateTime: 2019-09-19 16:24
 * @Description: 用户异常
 */
public class UserException extends BaseException {public UserException(ResultMsgStatus status, Throwable cause) {super(status, cause);
    }
    public UserException(ResultMsgStatus status) {super(status);
    }
    public UserException(String msg) {super(msg);
    }
    public UserException(ResultMsgStatus status, String msg) {super(status, msg);
    }
}

5. 全局异常处理

import com.bubble.salt.msg.ResultMsg;
import com.bubble.salt.msg.ResultMsgStatus;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {@ExceptionHandler(Exception.class)
    @ResponseBody
    public ResultMsg globalException(Exception e) {log.warn(e.getMessage(), e);
        // 更具自己定义的异常进行自自己需要的处理
        if (e instanceof UserException) {return new ResultMsg(ResultMsgStatus.TOKEN_INVALID, e.getMessage());
        }
        return ResultMsg.failure("服务器开小差了!");
    }
}
退出移动版