springboot3.x 统一返回值,统一异常处理.

1,本次我们通过自定义返回对象,然后再各个controller统一返回这个对象

新建如下泛型对象类

public class RsObject implements Serializable {

    private static final long serialVersionUID = 1L;

    /**
     * 业务返回状态码 200 代表成功
     */
    private Integer code = 200;

    /**
     * 返回消息
     */
    private String message = "success";

    /**
     * 返回的数据对象
     */
    private T  data;

    public RsObject ok(T data){
        this.setData(data);
        return this;
    }

    public boolean success(){
        return code == 200 ? true : false;
    }

    public RsObject error() {
        this.code = 500;
        this.message = "服务内部错误";
        return this;
    }

    public RsObject error(int code) {
        this.code = code;
        this.message = "";
        return this;
    }

    public RsObject error(int code, String message) {
        this.code = code;
        this.message = message;
        return this;
    }

    public RsObject error(String message) {
        this.code = 500;
        this.message = message;
        return this;
    }

    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;
    }
}

然后在需要的地方调用返回值即可,如登录请求

2,我们下面来编写和使用统一异常处理

核心模块是@RestControllerAdvice这个注解,我们定义一个全局的异常对象

/**
 * 统一异常处理
 */
@RestControllerAdvice
public class GlobalExceptionHandler {

    private static final Logger logger = LoggerFactory.getLogger(GlobalExceptionHandler.class);

    /**
     * 处理自定义异常
     */
    @ExceptionHandler(ServerException.class)
    public RsObject handleRenException(ServerException ex){
        RsObject result = new RsObject();
        result.error(ex.getCode(), ex.getMessage());

        return result;
    }

    @ExceptionHandler(DuplicateKeyException.class)
    public RsObject handleDuplicateKeyException(DuplicateKeyException ex){
        RsObject result = new RsObject();
        result.error(ErrorCode.DB_RECORD_EXISTS);

        return result;
    }

    @ExceptionHandler(Exception.class)
    public RsObject handleException(Exception ex){
        logger.error(ex.getMessage(), ex);

        return new RsObject().error();
    }
    
}

自定义异常类


public class ServerException extends RuntimeException{
    private static final long serialVersionUID = 1L;
    private int code;
    private String message;


    public ServerException(int code) {
        this.code = code;
        this.message = MessageUtils.getMessage(code);
    }

    public ServerException(int code, String... params) {
        this.code = code;
        this.message = MessageUtils.getMessage(code, params);
    }

    public ServerException(int code, Throwable e) {
        super(e);
        this.code = code;
        this.message = MessageUtils.getMessage(code);
    }

    public ServerException(int code, Throwable e, String... params) {
        super(e);
        this.code = code;
        this.message = MessageUtils.getMessage(code, params);
    }

    public ServerException(String message) {
        super(message);
        this.code = 500;
        this.message = message;
    }

    public ServerException(String message, Throwable e) {
        super(message, e);
        this.code = 500;
        this.message = message;
    }

    public String getMessage() {
        return message;
    }

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

    public int getCode() {
        return code;
    }

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

}

运行程序,触发异常如下


网页请求显示


另外后台的前端框架参考vue-pure-admin,使用其中的最简单版本


下一篇我要打通前后端,优先打通登录和用户信息

展开阅读全文

页面更新:2024-02-26

标签:异常   注解   全局   用户信息   后台   框架   模块   定义   对象   状态

1 2 3 4 5

上滑加载更多 ↓
推荐阅读:
友情链接:
更多:

本站资料均由网友自行发布提供,仅用于学习交流。如有版权问题,请与我联系,QQ:4156828  

© CopyRight 2020-2024 All Rights Reserved. Powered By 71396.com 闽ICP备11008920号-4
闽公网安备35020302034903号

Top