Jackson deserialize ISO8601 formatted date-time into Java8 Instant

后端 未结 5 1959
傲寒
傲寒 2021-02-03 18:02

I\'m trying to deserialize an ISO8601 formatted date into Java8 java.time.Instant using Jackson. I registered JavaTimeModule with the ObjectMapper, and turned off t

5条回答
  •  攒了一身酷
    2021-02-03 18:29

    In Jackson 2.9.8 (current one as I'm writing this) it's better to use Instant instead of Date.

    You have to add a dependency:

    
        com.fasterxml.jackson.datatype
        jackson-datatype-jsr310
        2.9.8
     
    

    Also, register the module and configure SerializationFeature.WRITE_DATES_AS_TIMESTAMPS to false.

    new ObjectMapper()
                    .findAndRegisterModules()
                    .configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
    

    More information about Jackson for Java8 here: https://github.com/FasterXML/jackson-modules-java8

提交回复
热议问题