api统一返回格式

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/Elison_zlj/article/details/80978386
Result.java


import java.io.Serializable;
import java.util.Map;

public class Result implements Serializable {
    private static final long serialVersionUID = -999362211833007653L;
    /**
     * 是否成功,成功找data、失败找msg
     */
    private final boolean success;

    /**
     * 返回结果编码,成功的话我喜欢设为0
     */
    private final int code;

    /**
     * 返回消息,一般放置可追溯的错误消息
     */
    private final String msg;


    /**
     * 返回数据
     */
    private final Object data;

    /**
     * 额外参数,不使用Map<String, Object>防止JSON解析时过于复杂
     */
    private final Map<String, String> extraInfo;

    private Result(boolean success, int code, String msg, Object data, Map<String, String> extraInfo) {
        this.success = success;
        this.code = code;
        this.msg = msg;
        this.data = data;
        this.extraInfo = extraInfo;
    }

    public static Result fail(int code, String msg, Map<String, String> extraInfo) {
        return new Result(false, code, msg, null, extraInfo);
    }

    public static Result fail(ResultCode resultCode, Map<String, String> extraInfo) {
        return new Result(false, resultCode.getCode(), resultCode.getMsg(), null, extraInfo);
    }

    public static Result success(Object data, Map<String, String> extraInfo) {
        return new Result(true, 0, null, data, extraInfo);
    }

    public static Result success(ResultCode resultCode, Object data, Map<String, String> extraInfo) {
        return new Result(true, resultCode.getCode(), resultCode.getMsg(), data, extraInfo);
    }

    public boolean isSuccess() {
        return success;
    }

    public int getCode() {
        return code;
    }

    public String getMsg() {
        return msg;
    }

    public Object getData() {
        return data;
    }

    public Map<String, String> getExtraInfo() {
        return extraInfo;
    }
}
ResultCode.java

public enum  ResultCode {
    /* 成功状态码 */
    SUCCESS(0, "成功"),

    /* 参数错误:10001-19999 */
    PARAM_IS_INVALID(10001, "参数无效"),
    PARAM_IS_BLANK(10002, "参数为空"),
    PARAM_TYPE_BIND_ERROR(10003, "参数类型错误"),
    PARAM_NOT_COMPLETE(10004, "参数缺失"),

    /* 用户错误:20001-29999*/
    USER_NOT_LOGGED_IN(20001, "用户未登录"),
    USER_LOGIN_ERROR(20002, "账号不存在或密码错误"),
    USER_ACCOUNT_FORBIDDEN(20003, "账号已被禁用"),
    USER_NOT_EXIST(20004, "用户不存在"),
    USER_HAS_EXISTED(20005, "用户已存在"),

    /* 业务错误:30001-39999 */
    SPECIFIED_QUESTIONED_USER_NOT_EXIST(30001, "某业务出现问题"),

    /* 系统错误:40001-49999 */
    SYSTEM_INNER_ERROR(40001, "系统繁忙,请稍后重试"),

    /* 数据错误:50001-599999 */
    RESULE_DATA_NONE(50001, "数据未找到"),
    DATA_IS_WRONG(50002, "数据有误"),
    DATA_ALREADY_EXISTED(50003, "数据已存在"),

    /* 接口错误:60001-69999 */
    INTERFACE_INNER_INVOKE_ERROR(60001, "内部系统接口调用异常"),
    INTERFACE_OUTTER_INVOKE_ERROR(60002, "外部系统接口调用异常"),
    INTERFACE_FORBID_VISIT(60003, "该接口禁止访问"),
    INTERFACE_ADDRESS_INVALID(60004, "接口地址无效"),
    INTERFACE_REQUEST_TIMEOUT(60005, "接口请求超时"),
    INTERFACE_EXCEED_LOAD(60006, "接口负载过高"),

    /* 权限错误:70001-79999 */
    PERMISSION_NO_ACCESS(70001, "无访问权限");

    private Integer code;
    private String msg;

    ResultCode(Integer code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public Integer getCode() {
        return code;
    }

    public String getMsg() {
        return msg;
    }
}
展开阅读全文

没有更多推荐了,返回首页