package com.aisi.template.domain; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; /** * 通用的 REST API 响应封装类 * * @param 返回数据的类型 */ @Data @AllArgsConstructor @NoArgsConstructor public class RestBean { /** 状态码 */ private int code; /** 提示消息 */ private String message; /** 具体数据 */ private V data; /** * 成功响应(默认使用 {@link RestCode#SUCCESS}) * * @param data 返回的数据 * @param 泛型参数 * @return RestBean 包装对象 */ public static RestBean success(V data) { return success(RestCode.SUCCESS, data); } /** * 成功响应(指定 RestCode 和数据) * * @param restCode 状态码枚举 * @param data 返回的数据 * @param 泛型参数 * @return RestBean 包装对象 */ public static RestBean success(RestCode restCode, V data) { return success(restCode.getCode(), restCode.getMessage(), data); } /** * 成功响应(只返回状态码和消息,不带数据) * * @param restCode 状态码枚举 * @param 泛型参数 * @return RestBean 包装对象 */ public static RestBean success(RestCode restCode) { return success(restCode, null); } /** * 成功响应(自定义 code 和 message) * * @param code 状态码 * @param message 提示消息 * @param data 返回的数据 * @param 泛型参数 * @return RestBean 包装对象 */ public static RestBean success(Integer code, String message, V data) { return new RestBean<>(code, message, data); } /** * 失败响应(默认使用 {@link RestCode#FAILURE}) * * @param data 返回的数据 * @param 泛型参数 * @return RestBean 包装对象 */ public static RestBean failure(V data) { return new RestBean<>(RestCode.FAILURE.getCode(), RestCode.FAILURE.getMessage(), data); } /** * 失败响应(指定 RestCode 和数据) * * @param restCode 状态码枚举 * @param data 返回的数据 * @param 泛型参数 * @return RestBean 包装对象 */ public static RestBean failure(RestCode restCode, V data) { return new RestBean<>(restCode.getCode(), restCode.getMessage(), data); } /** * 失败响应(只返回状态码和消息,不带数据) * * @param restCode 状态码枚举 * @param 泛型参数 * @return RestBean 包装对象 */ public static RestBean failure(RestCode restCode) { return new RestBean<>(restCode.getCode(), restCode.getMessage(), null); } /** * 失败响应(自定义 code 和 message) * * @param code 状态码 * @param message 提示消息 * @param data 返回的数据 * @param 泛型参数 * @return RestBean 包装对象 */ public static RestBean failure(int code, String message, V data) { return new RestBean<>(code, message, data); } }