Parent class:
@Data
public class ParentDAO {
@JsonDeserialize(using = CustomInstantDeserializer.class)
private final Instant estimatedTimeOfBerthing;
@JsonDeserialize(using = CustomInstantDeserializer.class)
private final Instant actualTimeOfBerthing;
}
Child class:
@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 class:
public class CustomInstantDeserializer extends JsonDeserializer<Instant> {
@Override
public Instant deserialize(JsonParser jsonparser, DeserializationContext context)
throws IOException {
return BpmsDateUtils.fromString(jsonparser.getText());
}
}
Sample of JSON from data layer (JsonSerialized):
{
"estimatedTimeOfBerthing": "2019-10-10T05:30:00+08:00",
"actualTimeOfBerthing": "2019-10-10T06:50:00+08:00",
"countOfCnr1": 30
"countOfCnr2": 40
}
In the scenario above, my API layer is retrieving data from the data layer(JSON).
Mapping into ParentDAO
has got no issues but when mapping into ChildDAO
, the following exception is thrown -> JSON parse error: Cannot deserialize value of type `java.time.Instant.
I kind of know where the issue lies in -> I'm passing in an Instant
to the child constructor without it having any knowledge of how to deserialize it. Anyone has got any ideas or have encountered the same issue? Any help would be greatly appreciated.