hecko84
hecko84

Reputation: 1324

Spring Boot: Default serialization for java.time.Duration changed from String to Number

We upgraded lately from Spring Boot 2.1.9 to 2.2.1 which caused our tests to fail. Investigation led to the result that the java.time.Duration type is now serialized differently by default. Instead of having the String "PT15M" in the JSON message we now get "900.0". The POJO definition looks like that

@JsonProperty(required = true, value = "duration")
@NotNull
private final Duration duration;

The question now is if there is some configuration property we can use to get the "old" behavior. I know we could also add annotation

@JsonFormat(shape = JsonFormat.Shape.STRING)

but I would prefer a way to have it just by configuration.

Upvotes: 7

Views: 3548

Answers (1)

Ananthapadmanabhan
Ananthapadmanabhan

Reputation: 6216

When you changed the version of spring-boot from 2.1.9 to 2.2.1 , there is also a change of version for the Jackson. From Spring-boot version 2.2 onwards the Jackson version is changed to 2.10. One of the changes that are part of this Jackson version change is the use of the flag SerializationFeature.WRITE_DURATIONS_AS_TIMESTAMPS for serializing the Duration time stamps instead of the earlier WRITE_DATES_AS_TIMESTAMPS.

By adding following property to the application.properties the service (and the serialization feature) will behave like pre 2.2

spring.jackson.serialization.write-durations-as-timestamps=false

Springboot 2.2 Changelist

Jackson 2.10 changelist

Jackson Issue tracker

Upvotes: 9

Related Questions