Chad Chad - 1 month ago 8
Java Question

Instant cannot be serialized to appropriate format even with jackson-datatype-jsr310

Adding the following dependencies:

compile("com.fasterxml.jackson.module:jackson-module-parameter-names:2.8.9")
compile("com.fasterxml.jackson.datatype:jackson-datatype-jdk8:2.8.9")
compile("com.fasterxml.jackson.datatype:jackson-datatype-jsr310:2.8.9")


I am now able to deserialize the following JSON with Instant:

{
"timestamp": {{$timestamp}}
}


to the following object:

Transaction(timestamp=2017-07-02T10:58:18Z)


with the following class declaration

data class Transaction(@NotNull val timestamp: Instant)


However, when immediately serializing afterwards, I get the following:

{
"timestamp": 1498992172
}


I am using Spring Boot with Kotlin.

Answer Source

You need to disable the feature WRITE_DATES_AS_TIMESTAMPS:

This can be achieved by adding the following in your application.yml file:

spring:
  jackson:
    serialization:
      WRITE_DATES_AS_TIMESTAMPS: false