首页 > 其他分享 >统一返回结果类

统一返回结果类

时间:2023-01-31 18:45:48浏览次数:29  
标签:返回 code return 结果 ResultsCode static Result public 统一

统一返回结果类

期望:

我们期望能有一个统一的RESTful接口返回形式, 它应该有一个统一的服务器返回状态码,一个统一的服务器端返回消息, 一个统一的返回数据集data
统一返回数据应如下示例

{
"code": 0,
"message": 'hello world',
"data": []
}

下面是实现:

Result.java


public class Result<T> implements Serializable {

    private static final long serialVersionUID = 1L;

    public int code;

    private String msg;

    private T data;

    private final Map<String, Object> mapdata = new HashMap<String, Object>();

    public void Result() {
        this.code = ResultsCode.SUCCESS.code;
        this.msg = ResultsCode.SUCCESS.message;
    }

    public Result<T> setCode(ResultsCode resultsCode) {
        this.code = resultsCode.code;
        return this;
    }

    public int getCode() {
        return code;
    }

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

    public String getMsg() {
        return msg;
    }

    public Result<T> setMsg(String msg) {
        this.msg = msg;
        return this;
    }

    public T getData() {
        return data;
    }

    public Result<T> setData(T data) {
        this.data = data;
        return this;
    }

    public Result<T> putDataValue(String key, Object value) {
        mapdata.put(key, value);
        this.data = (T) mapdata;
        return this;
    }

    @Override
    public String toString() {
        return "{" +
                "code=" + code +
                ", msg='" + msg + '\'' +
                ", data=" + data +
                '}';
    }
}

ResultsCode.java

public enum ResultsCode {

    SUCCESS(200, "成功"),

    FAIL(-1, "失败"),

    ERROR(500, "服务器异常"),

    UNAUTHORIZED(401, "未认证(签名错误)"),

    FORBIDDEN(403, "禁止访问"),

    NOT_FOUND(404, "接口不存在"),

    AUTH_ERROR(-10000, "鉴权登陆失败,请重新登录!");

    public int code;
    public String message;

    ResultsCode(int code, String message) {
        this.code = code;
        this.message = message;
    }
}

ResponseData.java

public class ResponseData {
    /****
     * 成功响应Msg中的信息
     **/
    private final static String SUCCESS = "success";

    /****
     * 失败响应Msg中的信息
     **/
    private final static String FAILED = "failed";

    private final static String FORBIDDEN = "forbidden";


    public static <T> Result<T> success(T data) {
        return new Result<T>().setCode(ResultsCode.SUCCESS).setMsg(SUCCESS).setData(data);
    }

    public static Result success() {
        return new Result().setCode(ResultsCode.SUCCESS).setMsg(SUCCESS);
    }

    public static Result forbidden() {
        return new Result().setCode(ResultsCode.FORBIDDEN).setMsg(FORBIDDEN);
    }

    public static Result authError() {
        return error(AUTH_ERROR);
    }

    public static <T> Result<T> error(ResultsCode resultsCode) {
        return new Result<T>().setCode(resultsCode.code).setMsg(resultsCode.message);
    }


    public static <T> Result<T> error(String mesage) {
        return new Result<T>().setMsg(mesage).setCode(ResultsCode.FAIL);
    }

    public static <T> Result<T> error(String mesage, ResultsCode resultsCode) {
        return new Result<T>().setMsg(mesage).setCode(resultsCode.code);
    }

    public static <T> Result<T> error() {
        return new Result<T>().setCode(ResultsCode.FAIL).setMsg(FAILED);
    }

    public static <T> Result<T> Forbidden() {
        return new Result<T>().setCode(ResultsCode.FORBIDDEN).setMsg(FORBIDDEN);
    }


}

标签:返回,code,return,结果,ResultsCode,static,Result,public,统一
From: https://www.cnblogs.com/zhangyouren/p/17080173.html

相关文章