|
|
|
@ -11,9 +11,9 @@ import java.time.Instant;
|
|
|
|
@ApiModel(description = "接口统一响应结构")
|
|
|
|
@ApiModel(description = "接口统一响应结构")
|
|
|
|
@Schema(description = "接口统一响应结构")
|
|
|
|
@Schema(description = "接口统一响应结构")
|
|
|
|
public class ApiResponse<T> {
|
|
|
|
public class ApiResponse<T> {
|
|
|
|
@Schema(description = "业务响应码,SUCCESS表示成功", example = "SUCCESS")
|
|
|
|
@Schema(description = "业务响应码;与 HTTP 状态码一致", example = "200")
|
|
|
|
private final String code;
|
|
|
|
private final int code;
|
|
|
|
@Schema(description = "业务响应消息", example = "success")
|
|
|
|
@Schema(description = "业务响应消息", example = "操作成功")
|
|
|
|
private final String message;
|
|
|
|
private final String message;
|
|
|
|
@Schema(description = "接口实际返回的业务数据")
|
|
|
|
@Schema(description = "接口实际返回的业务数据")
|
|
|
|
private final T data;
|
|
|
|
private final T data;
|
|
|
|
@ -22,18 +22,18 @@ public class ApiResponse<T> {
|
|
|
|
@Schema(description = "服务器生成响应的时间", example = "2026-08-05T21:00:00Z")
|
|
|
|
@Schema(description = "服务器生成响应的时间", example = "2026-08-05T21:00:00Z")
|
|
|
|
private final Instant timestamp;
|
|
|
|
private final Instant timestamp;
|
|
|
|
|
|
|
|
|
|
|
|
public ApiResponse(String code, String message, T data, String traceId, Instant timestamp) {
|
|
|
|
public ApiResponse(int code, String message, T data, String traceId, Instant timestamp) {
|
|
|
|
this.code = code;
|
|
|
|
this.code = code;
|
|
|
|
this.message = message;
|
|
|
|
this.message = message;
|
|
|
|
this.data = data;
|
|
|
|
this.data = data;
|
|
|
|
this.traceId = traceId;
|
|
|
|
this.traceId = traceId;
|
|
|
|
this.timestamp = timestamp;
|
|
|
|
this.timestamp = timestamp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
public String code() { return code; }
|
|
|
|
public int code() { return code; }
|
|
|
|
public String message() { return message; }
|
|
|
|
public String message() { return message; }
|
|
|
|
public T data() { return data; }
|
|
|
|
public T data() { return data; }
|
|
|
|
public String traceId() { return traceId; }
|
|
|
|
public String traceId() { return traceId; }
|
|
|
|
public Instant timestamp() { return timestamp; }
|
|
|
|
public Instant timestamp() { return timestamp; }
|
|
|
|
public static <T> ApiResponse<T> success(T data, String traceId) { return new ApiResponse<T>(ErrorCode.SUCCESS.name(), "操作成功", data, traceId, Instant.now()); }
|
|
|
|
public static <T> ApiResponse<T> success(T data, String traceId) { return new ApiResponse<T>(ErrorCode.SUCCESS.getCode(), "操作成功", data, traceId, Instant.now()); }
|
|
|
|
public static <T> ApiResponse<T> failure(ErrorCode errorCode, String message, String traceId) { return new ApiResponse<T>(errorCode.name(), message, null, traceId, Instant.now()); }
|
|
|
|
public static <T> ApiResponse<T> failure(ErrorCode errorCode, String message, String traceId) { return new ApiResponse<T>(errorCode.getCode(), message, null, traceId, Instant.now()); }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|