ServerResponse响应类:
import com.cxsz.ibosspro.common.constant.ResultCode; import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.annotation.JsonInclude; import lombok.Getter; import lombok.Setter; import java.io.Serializable; //保证序列化的时候,如果为null,key也会消失 @JsonInclude(JsonInclude.Include.NON_NULL) @Getter @Setter public class ServerResponse<T> implements Serializable { private int status; private String msg; private T data; public ServerResponse() { } public ServerResponse(int status){ this.status = status; } public ServerResponse(int status, String msg){ this.status = status; this.msg = msg; } public ServerResponse(int status, T data){ this.status = status; this.data = data; } public ServerResponse(int status, String msg, T data){ this.status = status; this.msg = msg; this.data = data; } //使之不在json序列化结果当中 @JsonIgnore public boolean isSuccess(){ return this.status == ResultCode.SUCCESS; } public int getStatus(){ return status; } public T getData(){ return data; } public String getMsg(){ return msg; } public static <T> ServerResponse<T> createBySuccess(){ return new ServerResponse<T>(ResultCode.SUCCESS); } public static <T> ServerResponse<T> createBySuccessMassage(String msg){ return new ServerResponse<T>(ResultCode.SUCCESS, msg); } public static <T> ServerResponse<T> createBySuccess(T data){ return new ServerResponse<T>(ResultCode.SUCCESS, data); } public static <T> ServerResponse<T> createBySuccess(String msg, T data){ return new ServerResponse<T>(ResultCode.SUCCESS, msg, data); } public static <T> ServerResponse<T> createByError(){ return new ServerResponse<T>(ResultCode.FAIL); } public static <T> ServerResponse<T> createByError(String msg){ return new ServerResponse<T>(ResultCode.FAIL, msg); } public static <T> ServerResponse<T> createByErrorCodeMessage(int errorcode, String msg){ return new ServerResponse<T>(errorcode, msg); } }
常量类:
/** * HTTP请求返回状态码 * * @author xxxx */ public class ResultCode { /** * 成功 */ public static final int SUCCESS = 0; /** * 失败 */ public static final int FAIL = -1; /** * 无权限 */ public static final int NO_AUTH = -2; }
原文地址:https://www.cnblogs.com/zhangbLearn/p/10105000.html
时间: 2024-10-10 15:04:28