统一返回结果类以及常量类

2024-06-04 13:25:09 浏览数 (1)

1、常量类

代码语言:java复制
public class Constants {
    public static final Integer SUCCESS_CODE = 200;
    public static final Integer FAILURE_CODE = 404;
    public static final String SUCCESS_MESSAGE = "success";
    public static final String FAIL_MESSAGE = "fail";
}

2、统一返回结果类

代码语言:java复制
public class Result {
    private String message;
    private Integer code;
    private Object data;

    public Result() {
    }

    public Result(String message, Integer code, Object data) {
        this.message = message;
        this.code = code;
        this.data = data;
    }

    public String getMessage() {
        return message;
    }

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

    public Integer getCode() {
        return code;
    }

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

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }

    public static Result success() {
        return new Result(Constants.SUCCESS_MESSAGE, Constants.SUCCESS_CODE, null);
    }

    public static Result success(Object data) {
        return new Result(Constants.SUCCESS_MESSAGE, Constants.SUCCESS_CODE, data);
    }

    public static Result fail() {
        return new Result(Constants.FAIL_MESSAGE, Constants.FAILURE_CODE, null);
    }

    public static Result fail(String message) {
        return new Result(message, Constants.FAILURE_CODE, null);
    }
}

0 人点赞