最近在使用sonar检查自己java代码的质量,也让我学习到了很多写法上的新东西,今天遇到了这样一个问题:
Fields in a "Serializable" class should either be transient or serializable
我的原代码这样的:这是个公共类,实现了序列化接口。
@Data
public class PespResponse<T> implements Serializable {
private String message;
private int code = ActionResultCode.SUCCESS.getValue();
private T data;
public PespResponse(){
this.code = ActionResultCode.SUCCESS.getValue();
this.message = "请求成功";
}
public PespResponse(int code,String msg){
this();
this.code = code;
this.message = msg;
}
public PespResponse(int code,String msg,T data){
this();
this.code = code;
this.message = msg;
this.data = data;
}
public PespResponse(T data){
this();
this.data = data;
}
}
但是在使用sonar质量分析的时候提示是:Fields in a "Serializable" class should either be transient or serializable squid:S1948
一时不知道怎么解决,查找了相关资料,于是有了下面这样的写法,特此做下笔记:
public class PespResponse<T extends Serializable> implements Serializable {
private String message;
private int code = ActionResultCode.SUCCESS.getValue();
private T data;
public PespResponse(){
this.code = ActionResultCode.SUCCESS.getValue();
this.message = "请求成功";
}