关于java:java-通用返回的对象记录

33次阅读

共计 934 个字符,预计需要花费 3 分钟才能阅读完成。

/**
 * 通用响应对象
 *
 * @param <T>
 */
@ToString
public class Resp<T> {
    private int code;
    private String msg;
    private T data;

    public Resp(CodeMsgEnum codeMsg, T data) {this.code = codeMsg.getCode();
        this.msg = codeMsg.getMsg();
        this.data = data;
    }

    public Resp(CodeMsgEnum codeMsg) {this.code = codeMsg.getCode();
        this.msg = codeMsg.getMsg();}

    public Resp(int code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public Resp(int code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public static <T> Resp OK(T data) {return new Resp(CodeMsgEnum.OK, data);
    }

    public static <T> Resp OK_WITHOUT_DATA() {return new Resp(CodeMsgEnum.OK);
    }

    public static <T> Resp ERROR(CodeMsgEnum codeMsg) {return new Resp(codeMsg, null);
    }

    public static <T> Resp ERROR(CodeMsgEnum codeMsg, T data) {return new Resp(codeMsg, data);
    }

    public int getCode() {return code;}

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

    public String getMsg() {return msg;}

    public void setMsg(String msg) {this.msg = msg;}

    public T getData() {return data;}

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

正文完
 0