使用fastjson反序列化数据不全;没想到设计上居然不报错
背景
fastjson 在反序列化一段json数据是总会丢失某个属性。
json数据
1 2 3 4 5 6 7 8 9
| { "time":1, "contract":{ "name":"1212", "notifyUrl":"http://localhost:20001", "userId":"1976220424287027200" } }
|
java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
| public class NotifyMqMsg {
private Contract contract;
private int time;
public NotifyMqMsg(Contract contract) { this.contract = contract; }
public Contract getContract() { return contract; }
public void setContract(Contract contract) { this.contract = contract; }
public int getTime() { return time; }
public void setTime(int time) { this.time = time; }
@Override public String toString() { return JSON.toJSONString(this); } }
|
每次反序列化的时候 time 属性始终为0.
解决
问题原因是 NotifyMqMsg 构造方法有问题,没有默认构造方法。但是fastjson居然不报错,jackson就会报错。大概是采用的方案不同