|
|
|
@ -5,75 +5,167 @@ import com.fasterxml.jackson.databind.annotation.JsonSerialize;
|
|
|
|
|
import lombok.AllArgsConstructor;
|
|
|
|
|
import lombok.Getter;
|
|
|
|
|
import lombok.NoArgsConstructor;
|
|
|
|
|
|
|
|
|
|
import java.io.Serializable;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 该类 `ServerResponse` 是本项目的通用的返回封装类,用于将服务端的操作结果统一进行封装后返回给客户端。
|
|
|
|
|
* 它可以包含操作的状态码、提示消息以及具体的数据内容(泛型 `T` 表示具体的数据类型),并且通过使用一些 Jackson 相关的注解以及定义的多个静态方法,
|
|
|
|
|
* 方便创建不同状态(成功或失败)下的响应实例,同时实现了 `Serializable` 接口,使得该类的对象可以在网络传输、对象持久化等场景下进行序列化和反序列化操作。
|
|
|
|
|
*
|
|
|
|
|
* @Author swg.
|
|
|
|
|
* @Date 2018/12/31 20:11
|
|
|
|
|
* @CONTACT 317758022@qq.com
|
|
|
|
|
* @DESC 作为本项目的通用的返回封装类
|
|
|
|
|
*/
|
|
|
|
|
@Getter
|
|
|
|
|
// 使用 Lombok 的 `@Getter` 注解,会自动为类中的 `status`、`msg` 和 `data` 属性生成对应的 `get` 方法,方便外部获取这些属性的值,遵循了 Java 的封装原则,简化了代码编写。
|
|
|
|
|
@JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL)
|
|
|
|
|
// 使用 Jackson 的 `@JsonSerialize` 注解,配置序列化时的行为,这里指定了只包含非 `null` 的属性进行序列化,
|
|
|
|
|
// 也就是说在将 `ServerResponse` 对象转换为 JSON 格式(例如返回给客户端时),如果 `msg` 或者 `data` 等属性为 `null`,则不会包含在序列化后的 JSON 数据中,减少不必要的数据传输。
|
|
|
|
|
public class ServerResponse<T> implements Serializable {
|
|
|
|
|
// 用于存储操作的状态码,通过与 `ResponseEnum` 枚举类中的状态码进行对比,可以判断操作是成功还是失败以及具体的状态类型,例如 `0` 表示成功等情况。
|
|
|
|
|
private int status;
|
|
|
|
|
// 用于存储提示消息,一般是对操作结果的文字描述,比如操作成功时可以是 "操作成功",失败时可以是具体的错误原因等,方便客户端理解服务端返回的结果含义。
|
|
|
|
|
private String msg;
|
|
|
|
|
// 泛型属性,用于存储具体的业务数据内容,类型根据实际业务场景而定,可以是单个对象、对象列表或者其他复杂的数据结构等,比如查询用户信息时可以是 `User` 对象,查询用户列表时可以是 `List<User>` 类型的数据。
|
|
|
|
|
private T data;
|
|
|
|
|
|
|
|
|
|
public ServerResponse(){}
|
|
|
|
|
public ServerResponse() {}
|
|
|
|
|
|
|
|
|
|
private ServerResponse(int status){
|
|
|
|
|
// 私有构造方法,接收一个状态码参数,用于创建只包含状态码的 `ServerResponse` 对象实例,通常在一些内部初始化或者特定场景下使用,
|
|
|
|
|
// 外部一般通过静态方法来创建完整的响应对象,该构造方法主要是为了代码复用和方便在类内部构建不同情况的对象。
|
|
|
|
|
private ServerResponse(int status) {
|
|
|
|
|
this.status = status;
|
|
|
|
|
}
|
|
|
|
|
private ServerResponse(int status,String msg){
|
|
|
|
|
|
|
|
|
|
// 私有构造方法,接收状态码和提示消息两个参数,用于创建包含状态码和提示消息的 `ServerResponse` 对象实例,
|
|
|
|
|
// 方便在已知操作状态码和对应的提示消息时构建响应对象,比如在出现错误时,传入错误状态码和具体的错误消息来创建相应的返回对象。
|
|
|
|
|
private ServerResponse(int status, String msg) {
|
|
|
|
|
this.status = status;
|
|
|
|
|
this.msg = msg;
|
|
|
|
|
}
|
|
|
|
|
private ServerResponse(int status,T data){
|
|
|
|
|
|
|
|
|
|
// 私有构造方法,接收状态码和业务数据两个参数,用于创建包含状态码和具体数据的 `ServerResponse` 对象实例,
|
|
|
|
|
// 常用于操作成功且有具体数据需要返回给客户端的场景,例如查询操作成功获取到了数据,就可以通过传入成功状态码和查询到的数据来构建响应对象。
|
|
|
|
|
private ServerResponse(int status, T data) {
|
|
|
|
|
this.status = status;
|
|
|
|
|
this.data = data;
|
|
|
|
|
}
|
|
|
|
|
private ServerResponse(int status,String msg,T data){
|
|
|
|
|
|
|
|
|
|
// 私有构造方法,接收状态码、提示消息和业务数据三个参数,用于创建包含完整信息(状态码、提示消息、业务数据)的 `ServerResponse` 对象实例,
|
|
|
|
|
// 适用于需要详细反馈操作结果,既包含状态、提示又有具体数据的各种复杂场景,比如部分更新操作,成功时可以传入成功状态码、更新成功的提示以及更新后的数据等情况来构建返回对象。
|
|
|
|
|
private ServerResponse(int status, String msg, T data) {
|
|
|
|
|
this.status = status;
|
|
|
|
|
this.msg = msg;
|
|
|
|
|
this.data = data;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 使用 `@JsonIgnore` 注解标记该方法,意味着在将 `ServerResponse` 对象进行序列化(如转换为 JSON 格式)时,这个方法不会被包含在内,
|
|
|
|
|
* 它用于判断当前响应对象是否表示操作成功,通过对比状态码和 `ResponseEnum.SUCCESS` 枚举常量对应的状态码(通常 `0` 表示成功)来返回布尔值结果,
|
|
|
|
|
* 方便外部代码快速判断操作是否成功,无需再去获取状态码后手动对比,提高了代码的可读性和便捷性。
|
|
|
|
|
*
|
|
|
|
|
* @return 返回 `true` 表示操作成功(状态码与成功状态码一致),返回 `false` 表示操作失败。
|
|
|
|
|
*/
|
|
|
|
|
@JsonIgnore
|
|
|
|
|
public boolean isSuccess(){
|
|
|
|
|
public boolean isSuccess() {
|
|
|
|
|
return this.status == ResponseEnum.SUCCESS.getCode();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 成功的方法
|
|
|
|
|
* 以下是一系列静态方法,用于创建表示操作成功的 `ServerResponse` 对象实例,方便在不同的成功场景下快速构建合适的返回对象。
|
|
|
|
|
*/
|
|
|
|
|
public static <T>ServerResponse<T> createBySuccess(){
|
|
|
|
|
return new ServerResponse<>(ResponseEnum.SUCCESS.getCode(),ResponseEnum.SUCCESS.getDesc());
|
|
|
|
|
}
|
|
|
|
|
public static <T>ServerResponse<T> createBySuccessMessage(String message){
|
|
|
|
|
return new ServerResponse<>(ResponseEnum.SUCCESS.getCode(),message);
|
|
|
|
|
}
|
|
|
|
|
public static <T>ServerResponse<T> createBySuccess(T data){
|
|
|
|
|
return new ServerResponse<>(ResponseEnum.SUCCESS.getCode(),data);
|
|
|
|
|
}
|
|
|
|
|
public static <T>ServerResponse<T> createBySuccess(String message,T data){
|
|
|
|
|
return new ServerResponse<>(ResponseEnum.SUCCESS.getCode(),message,data);
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 创建一个表示操作成功且只包含默认成功提示消息(从 `ResponseEnum.SUCCESS.getDesc()` 获取)的 `ServerResponse` 对象实例,
|
|
|
|
|
* 适用于不需要额外提供详细提示消息,仅告知客户端操作成功的简单场景,例如简单的查询操作成功,无其他特殊说明时可以使用该方法创建返回对象。
|
|
|
|
|
*
|
|
|
|
|
* @param <T> 泛型类型参数,由调用者指定具体的数据类型,使得该方法可以灵活适应不同业务数据类型的返回情况。
|
|
|
|
|
* @return 返回创建好的 `ServerResponse` 对象实例,状态码为成功状态码,提示消息为默认的成功描述信息。
|
|
|
|
|
*/
|
|
|
|
|
public static <T> ServerResponse<T> createBySuccess() {
|
|
|
|
|
return new ServerResponse<>(ResponseEnum.SUCCESS.getCode(), ResponseEnum.SUCCESS.getDesc());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 失败的方法
|
|
|
|
|
* 创建一个表示操作成功且包含自定义提示消息的 `ServerResponse` 对象实例,
|
|
|
|
|
* 通过传入自定义的 `message` 参数来指定具体的提示消息内容,适用于需要向客户端传达更详细的成功相关信息的场景,
|
|
|
|
|
* 比如新增数据成功后,可以传入 "数据新增成功,已保存到数据库" 这样的提示消息,让客户端更清楚具体的操作结果情况。
|
|
|
|
|
*
|
|
|
|
|
* @param <T> 泛型类型参数,由调用者指定具体的数据类型,与其他静态方法一样,可灵活适配不同业务数据返回情况。
|
|
|
|
|
* @param message 自定义的提示消息内容,用于替换默认的成功提示消息,更精准地告知客户端操作成功的相关细节。
|
|
|
|
|
* @return 返回创建好的 `ServerResponse` 对象实例,状态码为成功状态码,提示消息为传入的自定义消息。
|
|
|
|
|
*/
|
|
|
|
|
public static <T>ServerResponse<T> createByError(){
|
|
|
|
|
return new ServerResponse<>(ResponseEnum.ERROR.getCode(),ResponseEnum.ERROR.getDesc());
|
|
|
|
|
public static <T> ServerResponse<T> createBySuccessMessage(String message) {
|
|
|
|
|
return new ServerResponse<>(ResponseEnum.SUCCESS.getCode(), message);
|
|
|
|
|
}
|
|
|
|
|
public static <T>ServerResponse<T> createByErrorMessage(String msg){
|
|
|
|
|
return new ServerResponse<>(ResponseEnum.ERROR.getCode(),msg);
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 创建一个表示操作成功且包含具体业务数据的 `ServerResponse` 对象实例,
|
|
|
|
|
* 通过传入具体的数据对象 `data`(类型为泛型 `T`)来将业务数据封装到返回对象中,适用于查询等操作成功并获取到具体数据需要返回给客户端的场景,
|
|
|
|
|
* 例如查询用户信息成功后,将查询到的 `User` 对象作为参数传入,构建包含用户信息数据的返回对象传递给客户端。
|
|
|
|
|
*
|
|
|
|
|
* @param <T> 泛型类型参数,由调用者指定具体的数据类型,对应要返回的业务数据的实际类型。
|
|
|
|
|
* @param data 具体的业务数据对象,将被封装到 `ServerResponse` 对象的 `data` 属性中,作为操作成功的结果数据返回给客户端。
|
|
|
|
|
* @return 返回创建好的 `ServerResponse` 对象实例,状态码为成功状态码,数据为传入的具体业务数据。
|
|
|
|
|
*/
|
|
|
|
|
public static <T> ServerResponse<T> createBySuccess(T data) {
|
|
|
|
|
return new ServerResponse<>(ResponseEnum.SUCCESS.getCode(), data);
|
|
|
|
|
}
|
|
|
|
|
public static <T>ServerResponse<T> createByErrorCodeMessage(int code,String msg){
|
|
|
|
|
return new ServerResponse<>(code,msg);
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 创建一个表示操作成功且包含自定义提示消息和具体业务数据的 `ServerResponse` 对象实例,
|
|
|
|
|
* 同时传入 `message` 和 `data` 参数,分别用于指定详细的提示消息和具体的业务数据内容,适用于既需要告知客户端操作成功的详细情况,又有具体数据需要返回的复杂场景,
|
|
|
|
|
* 比如更新用户信息成功后,可以传入 "用户信息更新成功" 这样的提示消息以及更新后的 `User` 对象数据,构建完整的返回对象反馈给客户端。
|
|
|
|
|
*
|
|
|
|
|
* @param <T> 泛型类型参数,由调用者指定具体的数据类型,对应实际业务数据的类型以及返回对象中 `data` 属性的类型。
|
|
|
|
|
* @param message 自定义的提示消息内容,用于丰富操作成功的提示信息,让客户端更全面地了解操作结果情况。
|
|
|
|
|
* @param data 具体的业务数据对象,将被封装到 `ServerResponse` 对象的 `data` 属性中,作为操作成功的业务数据结果返回给客户端。
|
|
|
|
|
* @return 返回创建好的 `ServerResponse` 对象实例,状态码为成功状态码,包含传入的自定义提示消息和具体业务数据。
|
|
|
|
|
*/
|
|
|
|
|
public static <T> ServerResponse<T> createBySuccess(String message, T data) {
|
|
|
|
|
return new ServerResponse<>(ResponseEnum.SUCCESS.getCode(), message, data);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 以下是一系列静态方法,用于创建表示操作失败的 `ServerResponse` 对象实例,方便在不同的失败场景下快速构建合适的返回对象,向客户端反馈操作失败的情况及原因。
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 创建一个表示操作失败且只包含默认错误提示消息(从 `ResponseEnum.ERROR.getDesc()` 获取)的 `ServerResponse` 对象实例,
|
|
|
|
|
* 适用于一般性的操作失败情况,不需要详细说明具体错误原因,仅告知客户端操作失败时可以使用该方法创建返回对象,例如在出现未预期的异常等通用错误场景下使用。
|
|
|
|
|
*
|
|
|
|
|
* @param <T> 泛型类型参数,由调用者指定具体的数据类型,虽然操作失败时可能不一定有具体业务数据返回,但保持与其他方法一致的泛型定义,以保证方法通用性和灵活性。
|
|
|
|
|
* @return 返回创建好的 `ServerResponse` 对象实例,状态码为错误状态码,提示消息为默认的错误描述信息。
|
|
|
|
|
*/
|
|
|
|
|
public static <T> ServerResponse<T> createByError() {
|
|
|
|
|
return new ServerResponse<>(ResponseEnum.ERROR.getCode(), ResponseEnum.ERROR.getDesc());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
/**
|
|
|
|
|
* 创建一个表示操作失败且包含自定义错误提示消息的 `ServerResponse` 对象实例,
|
|
|
|
|
* 通过传入自定义的 `msg` 参数来指定具体的错误消息内容,适用于需要向客户端明确传达具体失败原因的场景,
|
|
|
|
|
* 比如参数校验不通过时,可以传入 "参数不符合要求,请检查输入参数" 这样的提示消息,让客户端清楚操作失败的具体缘由。
|
|
|
|
|
*
|
|
|
|
|
* @param <T> 泛型类型参数,由调用者指定具体的数据类型,同样是为了保证方法通用性,适配不同业务场景下的返回情况。
|
|
|
|
|
* @param msg 自定义的错误提示消息内容,用于替换默认的错误提示消息,更精准地告知客户端操作失败的原因所在。
|
|
|
|
|
* @return 返回创建好的 `ServerResponse` 对象实例,状态码为错误状态码,提示消息为传入的自定义消息。
|
|
|
|
|
*/
|
|
|
|
|
public static <T> ServerResponse<T> createByErrorMessage(String msg) {
|
|
|
|
|
return new ServerResponse<>(ResponseEnum.ERROR.getCode(), msg);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 创建一个表示操作失败且包含自定义状态码和错误提示消息的 `ServerResponse` 对象实例,
|
|
|
|
|
* 通过传入 `code` 和 `msg` 参数分别指定具体的状态码和错误提示消息内容,适用于需要更精细地表示不同类型的失败情况,
|
|
|
|
|
* 例如根据不同业务模块或者不同错误类型设置不同的状态码,并搭配相应的错误消息,让客户端能更准确地区分和理解操作失败的具体情况。
|
|
|
|
|
*
|
|
|
|
|
* @param <T> 泛型类型参数,由调用者指定具体的数据类型,保持方法通用性,可用于各种需要自定义状态码和消息返回的失败场景。
|
|
|
|
|
* @param code 自定义的状态码,用于更细致地标识不同的失败情况,与项目中定义的各种状态码规范相匹配,方便客户端根据状态码进行针对性处理。
|
|
|
|
|
* @param msg 自定义的错误提示消息内容,详细说明操作失败的原因,辅助客户端理解具体的问题所在。
|
|
|
|
|
* @return 返回创建好的 `ServerResponse` 对象实例,状态码为传入的自定义状态码,提示消息为传入的自定义消息。
|
|
|
|
|
*/
|
|
|
|
|
public static <T> ServerResponse<T> createByErrorCodeMessage(int code, String msg) {
|
|
|
|
|
return new ServerResponse<>(code, msg);
|
|
|
|
|
}
|
|
|
|
|
}
|