现象
今天在调http接口时,接收到的返回值一直是null,debug后发现系统抛了“No suitable constructor found for type 。。。”,说明这个类没有无参数构造函数(由于建了别的构造函数,覆盖了无参构造函数),抛异常后代码熔断,进入fallback(熔断处理方法)方法,这个方法默认返回null,所以这个对象为null,而且这个异常被捕捉了,所以日志上看不出来。
private List<Image> detailImages; //我在response中添加了这个属性
public static class Image {
private String smallUrl;
private String largeUrl;
private int largeUrlWidth;
private int largeUrlHeight;
//没有无参构造函数
public Image(String smallUrl, String largeUrl) {
this.smallUrl = smallUrl;
this.largeUrl = largeUrl;
}
public Image(String smallUrl, String largeUrl, int largeUrlWidth, int largeUrlHeight) {
this.smallUrl = smallUrl;
this.largeUrl = largeUrl;
this.largeUrlWidth = largeUrlWidth;
this.largeUrlHeight = largeUrlHeight;
}
public int getLargeUrlWidth() {
return largeUrlWidth;
}
public void setLargeUrlWidth(int largeUrlWidth) {
this.largeUrlWidth = largeUrlWidth;
}
public int getLargeUrlHeight() {
return largeUrlHeight;
}
public void setLargeUrlHeight(int largeUrlHeight) {
this.largeUrlHeight = largeUrlHeight;
}
public String getSmallUrl() {
return smallUrl;
}
public void setSmallUrl(String smallUrl) {
this.smallUrl = smallUrl;
}
public String getLargeUrl() {
return largeUrl;
}
public void setLargeUrl(String largeUrl) {
this.largeUrl = largeUrl;
}
}
原因
json在转对象反序列化的时候,需要先初始化对象,默认调用无参构造函数,再赋值,所以抛出异常。
解决办法
添加无参构造函数