I need to consume a Rest API using Java/Spring (RestTemplate). After doing some smoke test with Postman I see the dates fields have this structure
"clipStartDate": {
"__type": "Date",
"iso": "2010-09-14T00:00:00.000Z"
}
I tried to map this fields in my DTO using java.time.LocalDateTime.
But I'm getting a serialization exception. (org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot construct instance of java.time.LocalDateTime
)
What is the best practice in this case?
This error you're seeing means that your ObjectMapper
is not configured properly. In Spring Boot this comes autoconfigured out of the box, so if you use e.g Spring Boot 2.2 this error will disappear.
However if for some reason you don't have this possibility, then you need to configure an ObjectMapper
with an additional module called JavaTimeModule
.
@Bean
public ObjectMapper objectMapper(){
return new ObjectMapper()
.registerModule(new JavaTimeModule());
}
Here's a suplementary article describing how to further customize ObjectMapper