Why does jackson convert ZonedDateTime to decimal?

Viewed 817

I am trying to understand why does Jackson transform ZonedDateTime to Decimal while getting MvcResult in a spring intergration Test ?

Here is the expected date : 2019-10-01T10:10:44+02:00[Europe/Paris] and the actual result : 1572426644.000000000

3 Answers

Here is the answer that gives a precise solution to your question: Spring Data JPA - ZonedDateTime format for JSON serialization. In short, you need to annotate your ZonedDateTime as follows.

ZonedDateTime time = ZonedDateTime.now();

@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd'T'HH:mm:ss.SSSZ")
public ZonedDateTime getTime() {
    return time;
}

You also might need to add the dependency for JSR-310

<dependency>
    <groupId>com.fasterxml.jackson.datatype</groupId>
    <artifactId>jackson-datatype-jsr310</artifactId>
    <version>2.6.0</version>
</dependency>

I prevented the conversion by injecting MappingJackson2HttpMessageConverter in the Integration Test

MockMvcBuilders.standaloneSetup(myController).setMessageConverters(mappingJackson2HttpMessageConverter).build();
Related