How do I use Jackson JSON mapper with Java 8 LocalDateTime?
org.codehaus.jackson.map.JsonMappingException: Can not instantiate value of type [simple type, class java.time.LocalDateTime] from JSON String; no single-String constructor/factory method (through reference chain: MyDTO["field1"]->SubDTO["date"])
This maven dependency will solve your problem:
One thing I've struggled is that for ZonedDateTime timezone being changed to GMT during deserialization. Turned out, that by default jackson replaces it with one from context.. To keep zone one must disable this 'feature'
You may set this in your
application.yml
file to resolve Instant time, which is Date API in java8:I use this time format:
"{birthDate": "2018-05-24T13:56:13Z}"
to deserialize from json into java.time.Instant (see screenshot)This is just an example how to use it in a unit test that I hacked to debug this issue. The key ingredients are
Code: import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; import org.testng.Assert; import org.testng.annotations.Test;