@ToString
@NoArgsConstructor
@AllArgsConstructor
@Accessors(chain = true)
@ApiModel(value = "响应信息主体")
public class R 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 totals;
public static R ok() {
return restResult(null, CommonConstants.SUCCESS, "SUCCESS");
}
public static R ok(T data) {
return restResult(data, CommonConstants.SUCCESS, "SUCCESS");
}
public static R ok(T data, String msg) {
return restResult(data, CommonConstants.SUCCESS, msg);
}
public static R failed() {
return restResult(null, CommonConstants.FAIL, null);
}
public static R failed(String msg) {
return restResult(null, CommonConstants.FAIL, msg);
}
public static R failed(Integer code, String msg) {
return restResult(null, code, msg);
}
public static R failed(T data) {
return restResult(data, CommonConstants.FAIL, null);
}
public static R failed(LeaseException e) {
return restResult(null, e.getCode(), e.getMessage());
}
public static R failed(ErrorCode errorCode) {
return restResult(null, errorCode.getCode(), errorCode.getMessage());
}
public static R failed(T data, Integer code, String msg) {
return restResult(data, code, msg);
}
public static R failed(T data, ErrorCode errorCode) {
return restResult(data, errorCode.getCode(), errorCode.getMessage());
}
public static R failed(ErrorCode errorCode, String msg) {
return restResult(null, errorCode.getCode(), msg);
}
public static R failed(T data, String msg) {
return restResult(data, CommonConstants.FAIL, msg);
}
private static R restResult(T data, Integer code, String msg) {
R apiResult = new R<>();
apiResult.setCode(code);
apiResult.setData(data);
apiResult.setMsg(msg);
return apiResult;
}
public Boolean getSuccess() {
return ResultEnums.OK.getCode().equals(code);
}
}
ErrorCode:
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;
}