Reputation: 539
I'm trying to call a rest endpoint which returns a pojo object which looks like this:
public class Process {
@JsonProperty("id")
private String id = null;
@JsonProperty("processDefinitionId")
private String processDefinitionId = null;
@JsonProperty("businessKey")
private String businessKey = null;
@JsonProperty("startedAt")
private OffsetDateTime startedAt = null;
@JsonProperty("endedAt")
private OffsetDateTime endedAt = null;
@JsonProperty("durationInMs")
private Integer durationInMs = null;
@JsonProperty("startActivityDefinitionId")
private String startActivityDefinitionId = null;
@JsonProperty("endActivityDefinitionId")
private String endActivityDefinitionId = null;
@JsonProperty("startUserId")
private String startUserId = null;
@JsonProperty("deleteReason")
private String deleteReason = null;
//constructors and setters+getters
}
Here is the call:
ResponseEntity<Process> responseModel = restTemplate.exchange("http://localhost:8062/processes", HttpMethod.POST, httpEntity, Process.class);
The problem is that i've tried a few methods like ignoring the OffsetDateTime properties or trying to change the format of that date but it will throw this error:
com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of `org.threeten.bp.OffsetDateTime` (no Creators, like default construct, exist): no String-argument constructor/factory method to deserialize from String value ('2019-10-04T13:20:29.315Z')
Or it will return null :( What would be a good solution to solve this?
Upvotes: 4
Views: 6124
Reputation: 790
The error states it cannot construct instance of org.threeten.bp.OffsetDateTime. You need to use
java.time.offsetdatetime
Then in your model you can format it whatever way you like e.g.
@JsonProperty("endedAt") //this line is not needed when it is the same as the instance variable name
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd@HH:mm:ss.SSSZ")
private OffsetDateTime endedAt;
Upvotes: 6
Reputation: 41
I had the same problem with a bean generated by swagger. To solve it I created some serializer and deserializer for date types: org.threeten.bp.LocalDate and org.threeten.bp.OffsetDateTime. And it works well :).
@Bean
@Primary
public ObjectMapper serializingObjectMapper() {
ObjectMapper objectMapper = new ObjectMapper();
JavaTimeModule javaTimeModule = new JavaTimeModule();
javaTimeModule.addSerializer(OffsetDateTime.class, new OffsetDateTimeSerializer());
javaTimeModule.addDeserializer(OffsetDateTime.class, new OffsetDateTimeDeserializer());
javaTimeModule.addSerializer(LocalDate.class, new LocalDateSerializer());
javaTimeModule.addDeserializer(LocalDate.class, new LocalDateDeserializer());
objectMapper.registerModule(javaTimeModule);
return objectMapper;
}
public static class OffsetDateTimeSerializer extends JsonSerializer<OffsetDateTime> {
@Override
public void serialize(OffsetDateTime arg0, JsonGenerator arg1, SerializerProvider arg2) throws IOException {
arg1.writeString(arg0.toString());
}
}
public static class OffsetDateTimeDeserializer extends JsonDeserializer<OffsetDateTime> {
@Override
public OffsetDateTime deserialize(JsonParser arg0, DeserializationContext arg1) throws IOException {
return OffsetDateTime.parse(arg0.getText());
}
}
public static class LocalDateSerializer extends JsonSerializer<LocalDate> {
@Override
public void serialize(LocalDate arg0, JsonGenerator arg1, SerializerProvider arg2) throws IOException {
arg1.writeString(arg0.toString());
}
}
public static class LocalDateDeserializer extends JsonDeserializer<LocalDate> {
@Override
public LocalDate deserialize(JsonParser arg0, DeserializationContext arg1) throws IOException {
return LocalDate.parse(arg0.getText());
}
}
Upvotes: 2