|
@@ -39,13 +39,13 @@ public class CommonResult<T> implements Serializable {
|
|
|
/**
|
|
|
* 返回数据
|
|
|
*/
|
|
|
- private T result;
|
|
|
+ private T data;
|
|
|
/**
|
|
|
* 错误提示,用户可阅读
|
|
|
*
|
|
|
* @see ErrorCode#getMsg() ()
|
|
|
*/
|
|
|
- private String message;
|
|
|
+ private String msg;
|
|
|
|
|
|
/**
|
|
|
* 将传入的 result 对象,转换成另外一个泛型结果的对象
|
|
@@ -55,34 +55,50 @@ public class CommonResult<T> implements Serializable {
|
|
|
* @param <T> 返回的泛型
|
|
|
* @return 新的 CommonResult 对象
|
|
|
*/
|
|
|
- public static <T> CommonResult<T> error(CommonResult<?> result) {
|
|
|
- return error(result.getCode(), result.getMessage());
|
|
|
+ public static <T> CommonResult<T> fail(CommonResult<?> result) {
|
|
|
+ return fail(result.getCode(), result.getMsg());
|
|
|
}
|
|
|
|
|
|
- public static <T> CommonResult<T> error(Integer code, String message) {
|
|
|
+ public static <T> CommonResult<T> fail(Integer code, String message) {
|
|
|
Assert.isTrue(!GlobalErrorCode.SUCCESS.getCode().equals(code), "code 必须是错误的!");
|
|
|
CommonResult<T> result = new CommonResult<>();
|
|
|
result.code = code;
|
|
|
- result.message = message;
|
|
|
+ result.msg = message;
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
- public static <T> CommonResult<T> error(ErrorCode errorCode) {
|
|
|
- return error(errorCode.getCode(), errorCode.getMsg());
|
|
|
+ public static <T> CommonResult<T> fail(ErrorCode errorCode) {
|
|
|
+ return fail(errorCode.getCode(), errorCode.getMsg());
|
|
|
}
|
|
|
|
|
|
public static <T> CommonResult<T> success(T data) {
|
|
|
CommonResult<T> result = new CommonResult<>();
|
|
|
result.code = GlobalErrorCode.SUCCESS.getCode();
|
|
|
- result.result = data;
|
|
|
- result.message = "";
|
|
|
+ result.data = data;
|
|
|
+ result.msg = "";
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static <T> CommonResult<T> success(String message) {
|
|
|
+ CommonResult<T> result = new CommonResult<>();
|
|
|
+ result.code = GlobalErrorCode.SUCCESS.getCode();
|
|
|
+ result.data = null;
|
|
|
+ result.msg = message;
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static <T> CommonResult<T> success(T data, String message) {
|
|
|
+ CommonResult<T> result = new CommonResult<>();
|
|
|
+ result.code = GlobalErrorCode.SUCCESS.getCode();
|
|
|
+ result.data = data;
|
|
|
+ result.msg = message;
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
public static <T> CommonResult<T> success() {
|
|
|
CommonResult<T> result = new CommonResult<>();
|
|
|
result.code = GlobalErrorCode.SUCCESS.getCode();
|
|
|
- result.message = "";
|
|
|
+ result.msg = "";
|
|
|
return result;
|
|
|
}
|
|
|
|
|
@@ -110,20 +126,20 @@ public class CommonResult<T> implements Serializable {
|
|
|
return;
|
|
|
}
|
|
|
// 业务异常
|
|
|
- throw new BusinessException(code, message);
|
|
|
+ throw new BusinessException(code, msg);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 判断是否有异常。如果有,则抛出 {@link BusinessException} 异常
|
|
|
- * 如果没有,则返回 {@link #result} 数据
|
|
|
+ * 如果没有,则返回 {@link #data} 数据
|
|
|
*/
|
|
|
@JsonIgnore
|
|
|
public T getCheckedData() {
|
|
|
checkError();
|
|
|
- return result;
|
|
|
+ return data;
|
|
|
}
|
|
|
|
|
|
- public static <T> CommonResult<T> error(BusinessException businessException) {
|
|
|
- return error(businessException.getCode(), businessException.getMessage());
|
|
|
+ public static <T> CommonResult<T> fail(BusinessException businessException) {
|
|
|
+ return fail(businessException.getCode(), businessException.getMessage());
|
|
|
}
|
|
|
}
|