|
@@ -4,15 +4,18 @@ import java.io.Serializable;
|
|
|
import java.util.Objects;
|
|
|
|
|
|
import com.chelvc.framework.common.exception.FrameworkException;
|
|
|
+import com.chelvc.framework.common.exception.TerminatedException;
|
|
|
import com.fasterxml.jackson.annotation.JsonIgnore;
|
|
|
import lombok.EqualsAndHashCode;
|
|
|
import lombok.Getter;
|
|
|
import lombok.NonNull;
|
|
|
import lombok.ToString;
|
|
|
+import org.springframework.http.HttpStatus;
|
|
|
|
|
|
/**
|
|
|
* 请求结果对象
|
|
|
*
|
|
|
+ * @param <T> 结果数据类型
|
|
|
* @author Woody
|
|
|
* @date 2024/1/30
|
|
|
*/
|
|
@@ -53,9 +56,10 @@ public class Result<T> implements Serializable {
|
|
|
/**
|
|
|
* 构建成功结果
|
|
|
*
|
|
|
+ * @param <T> 结果数据类型
|
|
|
* @return 结果对象
|
|
|
*/
|
|
|
- public static <M> Result<M> success() {
|
|
|
+ public static <T> Result<T> success() {
|
|
|
return success(null);
|
|
|
}
|
|
|
|
|
@@ -63,9 +67,10 @@ public class Result<T> implements Serializable {
|
|
|
* 构建成功结果
|
|
|
*
|
|
|
* @param data 结果数据
|
|
|
+ * @param <T> 结果数据类型
|
|
|
* @return 结果对象
|
|
|
*/
|
|
|
- public static <M> Result<M> success(M data) {
|
|
|
+ public static <T> Result<T> success(T data) {
|
|
|
return success(data, "Success");
|
|
|
}
|
|
|
|
|
@@ -74,18 +79,20 @@ public class Result<T> implements Serializable {
|
|
|
*
|
|
|
* @param data 结果数据
|
|
|
* @param message 结果信息
|
|
|
+ * @param <T> 结果数据类型
|
|
|
* @return 结果对象
|
|
|
*/
|
|
|
- public static <M> Result<M> success(M data, String message) {
|
|
|
+ public static <T> Result<T> success(T data, String message) {
|
|
|
return of(OK, data, message);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 构建异常结果
|
|
|
*
|
|
|
+ * @param <T> 结果数据类型
|
|
|
* @return 结果对象
|
|
|
*/
|
|
|
- public static <M> Result<M> failure() {
|
|
|
+ public static <T> Result<T> failure() {
|
|
|
return failure("Failure");
|
|
|
}
|
|
|
|
|
@@ -93,20 +100,22 @@ public class Result<T> implements Serializable {
|
|
|
* 构建异常结果
|
|
|
*
|
|
|
* @param message 异常消息
|
|
|
+ * @param <T> 结果数据类型
|
|
|
* @return 结果对象
|
|
|
*/
|
|
|
- public static <M> Result<M> failure(String message) {
|
|
|
+ public static <T> Result<T> failure(String message) {
|
|
|
return of(ERROR, null, message);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 构建异常结果
|
|
|
- * param data 结果数据
|
|
|
*
|
|
|
+ * @param data 结果数据
|
|
|
* @param message 异常消息
|
|
|
+ * @param <T> 结果数据类型
|
|
|
* @return 结果对象
|
|
|
*/
|
|
|
- public static <M> Result<M> failure(M data, String message) {
|
|
|
+ public static <T> Result<T> failure(T data, String message) {
|
|
|
return of(ERROR, data, message);
|
|
|
}
|
|
|
|
|
@@ -125,9 +134,10 @@ public class Result<T> implements Serializable {
|
|
|
*
|
|
|
* @param code 状态码
|
|
|
* @param data 结果数据
|
|
|
+ * @param <T> 结果数据类型
|
|
|
* @return 结果对象
|
|
|
*/
|
|
|
- public static <M> Result<M> of(@NonNull String code, M data) {
|
|
|
+ public static <T> Result<T> of(@NonNull String code, T data) {
|
|
|
return of(code, data, null);
|
|
|
}
|
|
|
|
|
@@ -137,10 +147,11 @@ public class Result<T> implements Serializable {
|
|
|
* @param code 状态码
|
|
|
* @param data 结果数据
|
|
|
* @param message 结果信息
|
|
|
+ * @param <T> 结果数据类型
|
|
|
* @return 结果对象
|
|
|
*/
|
|
|
- public static <M> Result<M> of(@NonNull String code, M data, String message) {
|
|
|
- Result<M> result = new Result<>();
|
|
|
+ public static <T> Result<T> of(@NonNull String code, T data, String message) {
|
|
|
+ Result<T> result = new Result<>();
|
|
|
result.code = code;
|
|
|
result.data = data;
|
|
|
result.message = message;
|
|
@@ -148,22 +159,17 @@ public class Result<T> implements Serializable {
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 是否成功
|
|
|
- *
|
|
|
- * @return true/false
|
|
|
- */
|
|
|
- @JsonIgnore
|
|
|
- public boolean isSuccess() {
|
|
|
- return Objects.equals(this.code, OK);
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 是否失败
|
|
|
+ * 获取Http状态码
|
|
|
*
|
|
|
- * @return true/false
|
|
|
+ * @return Http状态码
|
|
|
*/
|
|
|
@JsonIgnore
|
|
|
- public boolean isFailure() {
|
|
|
- return Objects.equals(this.code, ERROR);
|
|
|
+ public HttpStatus status() {
|
|
|
+ if (Objects.equals(this.code, OK) || Objects.equals(this.code, TerminatedException.CODE)) {
|
|
|
+ return HttpStatus.OK;
|
|
|
+ } else if (Objects.equals(this.code, ERROR)) {
|
|
|
+ return HttpStatus.INTERNAL_SERVER_ERROR;
|
|
|
+ }
|
|
|
+ return HttpStatus.BAD_REQUEST;
|
|
|
}
|
|
|
}
|