From 388f37de6c39f971270112295e31ea65653a4e6e Mon Sep 17 00:00:00 2001 From: wangbing Date: Tue, 9 Sep 2025 14:18:58 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=8A=E4=BC=A0=E5=A4=87=E4=BB=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/xyz/wbsite/achat/tools/Result.java | 209 ++++++++++++++++++ 1 file changed, 209 insertions(+) create mode 100644 src/main/java/xyz/wbsite/achat/tools/Result.java diff --git a/src/main/java/xyz/wbsite/achat/tools/Result.java b/src/main/java/xyz/wbsite/achat/tools/Result.java new file mode 100644 index 0000000..b910f79 --- /dev/null +++ b/src/main/java/xyz/wbsite/achat/tools/Result.java @@ -0,0 +1,209 @@ +package xyz.wbsite.achat.tools; + +import java.util.HashMap; +import java.util.Map; + +/** + * 接口响应结果基类 + * 泛型支持的数据响应封装,提供统一的响应格式和错误处理 + * + * @author wangbing + * @version 0.0.1 + * @since 1.8 + */ +public class Result { + + /** + * 响应状态码 + */ + private int code = 200; + + /** + * 响应消息 + */ + private String message = "success"; + + /** + * 响应数据 + */ + private T data; + + /** + * 是否成功 + */ + private boolean success = true; + + /** + * 错误详情,用于表单验证等场景 + */ + private Map errors; + + /** + * 请求ID,用于问题追踪 + */ + private String requestId; + + /** + * 响应时间戳 + */ + private long timestamp = System.currentTimeMillis(); + + public int getCode() { + return code; + } + + public Result setCode(int code) { + this.code = code; + return this; + } + + public String getMessage() { + return message; + } + + public Result setMessage(String message) { + this.message = message; + return this; + } + + public T getData() { + return data; + } + + public Result setData(T data) { + this.data = data; + return this; + } + + public boolean isSuccess() { + return success; + } + + public Result setSuccess(boolean success) { + this.success = success; + return this; + } + + public Map getErrors() { + return errors; + } + + public Result setErrors(Map errors) { + this.errors = errors; + return this; + } + + public String getRequestId() { + return requestId; + } + + public Result setRequestId(String requestId) { + this.requestId = requestId; + return this; + } + + public long getTimestamp() { + return timestamp; + } + + public Result setTimestamp(long timestamp) { + this.timestamp = timestamp; + return this; + } + + /** + * 添加字段级别的错误信息 + * + * @param field 字段名 + * @param error 错误信息 + * @return 当前结果对象,支持链式调用 + */ + public Result addError(String field, String error) { + if (errors == null) { + errors = new HashMap<>(); + } + errors.put(field, error); + return this; + } + + /** + * 返回成功信息 + * + * @return 结果 + */ + public static Result success() { + return new Result<>(); + } + + /** + * 返回带数据的成功信息 + * + * @param data 响应数据 + * @return 结果 + */ + public static Result success(T data) { + Result result = new Result<>(); + result.setData(data); + return result; + } + + /** + * 返回错误信息 + * + * @param message 错误信息 + * @return 错误信息对象 + */ + public static Result error(String message) { + Result result = new Result<>(); + result.message = message; + result.code = 500; + result.success = false; + return result; + } + + /** + * 返回错误信息 + * + * @param code 错误码 + * @param message 错误信息 + * @return 错误信息对象 + */ + public static Result error(int code, String message) { + Result result = new Result<>(); + result.code = code; + result.message = message; + result.success = false; + return result; + } + + /** + * 返回带数据的错误信息 + * + * @param code 错误码 + * @param message 错误信息 + * @param data 错误相关数据 + * @return 错误信息对象 + */ + public static Result error(int code, String message, T data) { + Result result = new Result<>(); + result.code = code; + result.message = message; + result.success = false; + result.data = data; + return result; + } + + /** + * 从异常创建错误响应 + * + * @param e 异常 + * @return 错误信息对象 + */ + public static Result error(Exception e) { + Result result = new Result<>(); + result.code = 500; + result.message = e.getMessage() != null ? e.getMessage() : "系统异常"; + result.success = false; + return result; + } +} \ No newline at end of file