/**
* 响应信息主体
*/
@ToString
@NoArgsConstructor
@AllArgsConstructor
@Accessors(chain = true)
@ApiModel(value = "响应信息主体")
public class R<T> implements Serializable {
private static final long serialVersionUID = 1L;
@Getter
@Setter
@ApiModelProperty(value = "返回标记:成功标记=0,失败标记=1")
private Integer code;
@Getter
@Setter
@ApiModelProperty(value = "返回信息")
private String msg;
@Getter
@Setter
@ApiModelProperty(value = "数据")
private T data;
@Getter
@Setter
@ApiModelProperty(value = "数据")
private Map<String, Object> totals;
public static <T> R<T> ok() {
return restResult(null, CommonConstants.SUCCESS, "SUCCESS");
}
public static <T> R<T> ok(T data) {
return restResult(data, CommonConstants.SUCCESS, "SUCCESS");
}
public static <T> R<T> ok(T data, String msg) {
return restResult(data, CommonConstants.SUCCESS, msg);
}
public static <T> R<T> failed() {
return restResult(null, CommonConstants.FAIL, null);
}
public static <T> R<T> failed(String msg) {
return restResult(null, CommonConstants.FAIL, msg);
}
public static <T> R<T> failed(Integer code, String msg) {
return restResult(null, code, msg);
}
public static <T> R<T> failed(T data) {
return restResult(data, CommonConstants.FAIL, null);
}
public static <T> R<T> failed(LeaseException e) {
return restResult(null, e.getCode(), e.getMessage());
}
public static <T> R<T> failed(ErrorCode errorCode) {
return restResult(null, errorCode.getCode(), errorCode.getMessage());
}
public static <T> R<T> failed(T data, Integer code, String msg) {
return restResult(data, code, msg);
}
public static <T> R<T> failed(T data, ErrorCode errorCode) {
return restResult(data, errorCode.getCode(), errorCode.getMessage());
}
public static <T> R<T> failed(ErrorCode errorCode, String msg) {
return restResult(null, errorCode.getCode(), msg);
}
public static <T> R<T> failed(T data, String msg) {
return restResult(data, CommonConstants.FAIL, msg);
}
private static <T> R<T> restResult(T data, Integer code, String msg) {
R<T> apiResult = new R<>();
apiResult.setCode(code);
apiResult.setData(data);
apiResult.setMsg(msg);
return apiResult;
}
public Boolean getSuccess() {
return ResultEnums.OK.getCode().equals(code);
}
}
ErrorCode:
/**
* 错误编码,由5位数字组成,前2位为模块编码,后3位为业务编码
* 如:10001(10代表系统模块,001代表业务代码)
*/
public interface ErrorCode {
Integer getCode();
String getMessage();
int INTERNAL_SERVER_ERROR = 500;
int UNAUTHORIZED = 401;
int FORBIDDEN = 403;
int NOT_NULL = 10001;
}
ErrorCodeEnum:
/**
*/
@AllArgsConstructor
@Getter
public enum ErrorCodeEnum implements ErrorCode {
OK(200, ""),
QUERY_ORDER_PHONE_IS_NOT_NULL(20000, "查询订单的手机号不能为空"),
;
/**
* 类型
*/
private final Integer code;
/**
* 报错信息
*/
private final String message;
}
ResultEnums:
@AllArgsConstructor
@Getter
public enum ResultEnums {
// 成功
OK(0, "SUCCESS"),
// 失败
ERROR(1, "ERROR"),
;
/**
* 值
*/
private final Integer code;
/**
* 名称
*/
private final String name;
CommonConstants:
public interface CommonConstants {
/**
* 成功标记
*/
Integer SUCCESS = 0;
/**
* 失败标记
*/
Integer FAIL = 1;
}