|
|
|
@ -14,65 +14,91 @@ import java.io.Serializable;
|
|
|
|
|
* @CONTACT 317758022@qq.com
|
|
|
|
|
* @DESC 作为本项目的通用的返回封装类
|
|
|
|
|
*/
|
|
|
|
|
// 使用lombok的@Getter注解,会自动为类中的非静态成员变量(这里的status、msg、data)生成对应的Getter方法,方便在其他地方获取这些变量的值,减少了手动编写Getter方法的代码量。
|
|
|
|
|
@Getter
|
|
|
|
|
// 使用lombok的@NoArgsConstructor注解,会为该类生成一个无参构造函数,方便在一些需要默认构造实例的场景下使用,比如反序列化等操作。
|
|
|
|
|
@NoArgsConstructor
|
|
|
|
|
// 使用Jackson的@JsonSerialize注解来配置序列化相关的行为,这里设置include属性为JsonSerialize.Inclusion.NON_NULL,表示在将对象序列化为JSON格式时,只包含非空的属性,忽略值为null的属性,有助于减少网络传输的数据量以及使返回的JSON数据更加简洁。
|
|
|
|
|
@JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL)
|
|
|
|
|
// 该类实现了Serializable接口,意味着这个类的实例可以被序列化和反序列化,常用于在网络传输、持久化存储等场景中保存和恢复对象的状态。
|
|
|
|
|
public class ServerResponse<T> implements Serializable {
|
|
|
|
|
// 用于存储响应的状态码,不同的状态码代表不同的响应情况,例如成功、失败、参数错误等,通过构造函数等方式进行赋值。
|
|
|
|
|
private int status;
|
|
|
|
|
// 用于存储响应的提示消息,比如成功时的提示信息或者失败时具体的错误描述等,方便客户端了解具体的响应含义。
|
|
|
|
|
private String msg;
|
|
|
|
|
// 定义了一个泛型变量data,用于存储具体的业务数据,其类型根据实际使用场景确定(由创建ServerResponse实例时传入的具体类型决定),可以是单个对象、集合等各种类型的数据。
|
|
|
|
|
private T data;
|
|
|
|
|
|
|
|
|
|
// 私有构造函数,只接收一个状态码参数,用于在特定场景下创建ServerResponse实例时,只初始化状态码,其他属性保持默认值(null或初始值),一般在内部逻辑根据具体情况来灵活构建响应对象时使用。
|
|
|
|
|
private ServerResponse(int status){
|
|
|
|
|
this.status = status;
|
|
|
|
|
}
|
|
|
|
|
// 私有构造函数,接收状态码和提示消息两个参数,用于创建带有指定状态码和对应提示消息的ServerResponse实例,方便在需要明确返回错误消息或者特定提示信息的场景下构建响应对象。
|
|
|
|
|
private ServerResponse(int status,String msg){
|
|
|
|
|
this.status = status;
|
|
|
|
|
this.msg = msg;
|
|
|
|
|
}
|
|
|
|
|
// 私有构造函数,接收状态码和业务数据两个参数,用于创建带有指定状态码以及对应业务数据的ServerResponse实例,在业务处理成功且需要返回具体数据时可使用该构造函数来构建响应对象。
|
|
|
|
|
private ServerResponse(int status,T data){
|
|
|
|
|
this.status = status;
|
|
|
|
|
this.data = data;
|
|
|
|
|
}
|
|
|
|
|
// 私有构造函数,接收状态码、提示消息以及业务数据三个参数,用于创建一个完整的包含状态码、提示消息和业务数据的ServerResponse实例,适用于各种综合场景下灵活构建响应对象。
|
|
|
|
|
private ServerResponse(int status,String msg,T data){
|
|
|
|
|
this.status = status;
|
|
|
|
|
this.msg = msg;
|
|
|
|
|
this.data = data;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 使用Jackson的@JsonIgnore注解,标记该方法在对象序列化时会被忽略,不会被转换为JSON格式的数据。
|
|
|
|
|
// 这个方法用于判断当前响应是否表示成功,通过比较状态码和ResponseEnum.SUCCESS.getCode()(即表示成功的状态码)是否相等来返回判断结果,方便在其他地方快速判断响应是否成功。
|
|
|
|
|
@JsonIgnore
|
|
|
|
|
public boolean isSuccess(){
|
|
|
|
|
return this.status == ResponseEnum.SUCCESS.getCode();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 以下是一系列静态方法,用于创建表示成功的ServerResponse实例,方便在业务处理成功的不同场景下快速构建相应的响应对象返回给客户端。
|
|
|
|
|
*/
|
|
|
|
|
/**
|
|
|
|
|
* 成功的方法
|
|
|
|
|
*/
|
|
|
|
|
// 创建一个表示成功的ServerResponse实例,使用ResponseEnum.SUCCESS中定义的状态码和描述信息来初始化,适用于只需要简单告知客户端操作成功的场景,没有额外的提示消息和具体业务数据返回。
|
|
|
|
|
public static <T>ServerResponse<T> createBySuccess(){
|
|
|
|
|
return new ServerResponse<>(ResponseEnum.SUCCESS.getCode(),ResponseEnum.SUCCESS.getDesc());
|
|
|
|
|
}
|
|
|
|
|
// 创建一个表示成功的ServerResponse实例,接收一个自定义的消息参数message,使用ResponseEnum.SUCCESS的状态码以及传入的message来初始化,用于在成功的同时需要向客户端返回特定提示消息的场景。
|
|
|
|
|
public static <T>ServerResponse<T> createBySuccessMessage(String message){
|
|
|
|
|
return new ServerResponse<>(ResponseEnum.SUCCESS.getCode(),message);
|
|
|
|
|
}
|
|
|
|
|
// 创建一个表示成功的ServerResponse实例,接收一个泛型参数data,使用ResponseEnum.SUCCESS的状态码以及传入的数据来初始化,用于在业务处理成功且有具体业务数据需要返回给客户端的场景。
|
|
|
|
|
public static <T>ServerResponse<T> createBySuccess(T data){
|
|
|
|
|
return new ServerResponse<>(ResponseEnum.SUCCESS.getCode(),data);
|
|
|
|
|
}
|
|
|
|
|
// 创建一个表示成功的ServerResponse实例,接收自定义的消息参数message和泛型参数data,使用ResponseEnum.SUCCESS的状态码、传入的message以及data来初始化,适用于成功且既有提示消息又有业务数据需要返回的综合场景。
|
|
|
|
|
public static <T>ServerResponse<T> createBySuccess(String message,T data){
|
|
|
|
|
return new ServerResponse<>(ResponseEnum.SUCCESS.getCode(),message,data);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 以下是一系列静态方法,用于创建表示失败的ServerResponse实例,方便在业务处理出现问题的不同场景下快速构建相应的响应对象返回给客户端。
|
|
|
|
|
*/
|
|
|
|
|
/**
|
|
|
|
|
* 失败的方法
|
|
|
|
|
*/
|
|
|
|
|
// 创建一个表示失败的ServerResponse实例,使用ResponseEnum.ERROR中定义的状态码和描述信息来初始化,适用于一般性的错误情况,只告知客户端操作失败,没有更详细的错误消息定制。
|
|
|
|
|
public static <T>ServerResponse<T> createByError(){
|
|
|
|
|
return new ServerResponse<>(ResponseEnum.ERROR.getCode(),ResponseEnum.ERROR.getDesc());
|
|
|
|
|
}
|
|
|
|
|
// 创建一个表示失败的ServerResponse实例,接收一个自定义的错误消息参数msg,使用ResponseEnum.ERROR的状态码以及传入的msg来初始化,用于在出现错误且需要向客户端返回具体错误描述的场景。
|
|
|
|
|
public static <T>ServerResponse<T> createByErrorMessage(String msg){
|
|
|
|
|
return new ServerResponse<>(ResponseEnum.ERROR.getCode(),msg);
|
|
|
|
|
}
|
|
|
|
|
// 创建一个表示失败的ServerResponse实例,接收状态码和自定义的错误消息两个参数,用于在需要根据具体业务逻辑返回特定状态码以及对应错误消息的场景下构建响应对象,更加灵活地表示不同类型的失败情况。
|
|
|
|
|
public static <T>ServerResponse<T> createByErrorCodeMessage(int code,String msg){
|
|
|
|
|
return new ServerResponse<>(code,msg);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|