父类:
@Data
public class ParentDAO {
@JsonDeserialize(using = CustomInstantDeserializer.class)
private final Instant estimatedTimeOfberthing;
@JsonDeserialize(using = CustomInstantDeserializer.class)
private final Instant actualTimeOfberthing;
}
儿童班:
@Data
public class ChildDAO extends ParentDAO {
private final int countOfCnr1;
private final int countOfCnr2;
@Builder
public Child(Instant estimatedTimeOfberthing,Instant actualTimeOfberthing,int countOfCnr1,int countOfCnr2) {
super(estimatedTimeOfberthing,actualTimeOfberthing);
this.countOfCnr1= countOfCnr1;
this.countOfCnr2= countOfCnr2;
}
}
CustomInstantDeserializer类:
public class CustomInstantDeserializer extends JsonDeserializer<Instant> {
@Override
public Instant deserialize(JsonParser jsonparser,DeserializationContext context)
throws IOException {
return BpmsDateUtils.fromString(jsonparser.getText());
}
}
数据层中的JSON样本(JsonSerialized):
{
"estimatedTimeOfberthing": "2019-10-10T05:30:00+08:00","actualTimeOfberthing": "2019-10-10T06:50:00+08:00","countOfCnr1": 30
"countOfCnr2": 40
}
在上述情况下,我的API层正在从数据层(JSON)检索数据。
映射到ParentDAO
没问题,但是映射到ChildDAO
时,抛出以下异常-> JSON解析错误:无法反序列化类型java.time.Instant的值。
我有点知道问题出在哪里->我正在将Instant
传递给子构造函数,但它不知道如何反序列化。任何人有任何想法或遇到相同的问题?任何帮助将不胜感激。