public enum ResponseCode {
/**
* 成功
*/
SUCCESS(200, true),
/**
* 失败
*/
FAILURE(500, false),
/**
* 错误
*/
ERROR(-1, false);
/**
* 状 态 码
*/
@Getter
private final int code;
/**
* 携 带 消 息
*/
@Getter
private final boolean success;
/**
* 构 造 方 法
*/
ResponseCode(int code, boolean success) {
this.code = code;
this.success = success;
}
}
@Data
public class Response<T> implements Serializable {
/**
* 状态码
*/
private int code;
/**
* 返回消息
*/
private boolean success;
/**
* 返回数据
*/
private T data;
public Response() {
}
public static <T> Response<T> success() {
return Response.success(ResponseCode.SUCCESS.getCode(), ResponseCode.SUCCESS.isSuccess(), null);
}
public static <T> Response<T> failure() {
return Response.failure(ResponseCode.FAILURE.getCode(), ResponseCode.FAILURE.isSuccess(), null);
}
public static <T> Response<T> error() {
return Response.error(ResponseCode.ERROR.getCode(), ResponseCode.ERROR.isSuccess(), null);
}
public static <T> Response<T> success(T data) {
return Response.success(ResponseCode.SUCCESS.getCode(), ResponseCode.SUCCESS.isSuccess(), data);
}
public static <T> Response<T> failure(T data) {
return Response.failure(ResponseCode.FAILURE.getCode(), ResponseCode.FAILURE.isSuccess(), data);
}
public static <T> Response<T> error(T data) {
return Response.error(ResponseCode.ERROR.getCode(), ResponseCode.ERROR.isSuccess(), null);
}
/**
* 成功操作, 携带自定义状态码、消息、数据
*/
public static <T> Response<T> success(int code, boolean success, T data) {
Response<T> result = new Response<T>();
result.setCode(code);
result.setSuccess(success);
result.setData(data);
return result;
}
/**
* 失败操作, 携带自定义状态码、消息、数据
*/
public static <T> Response<T> failure(int code, boolean success, T data) {
Response<T> result = new Response<T>();
result.setCode(code);
result.setSuccess(success);
result.setData(data);
return result;
}
/**
* 错误操作, 携带自定义状态码、消息、数据
*/
public static <T> Response<T> error(int code, boolean success, T data) {
Response<T> result = new Response<T>();
result.setCode(code);
result.setSuccess(success);
result.setData(data);
return result;
}
}