I'm using spring boot 2.3.4.RELEASE and when trying to convert a DTO containing an Instant attribute to JSON format, the jackson ObjectMapper keeps converting it to timestamp format, even with write-dates-as-timestamps option turned off.
pom.xml
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
....
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
</dependency>
application.properties
spring.jackson.serialization.write-dates-as-timestamps = false
DTO:
@Data
public class MyDTO {
Long id;
@NotNull
String number;
@NotNull
Integer year;
@NotNull
VesselContractStatusEnum status;
Instant statusDate;
}
Rest Controller response:
{
"id": 1,
"number": "202000",
"year": 2020,
"status": "Open",
"statusDate": 1602298800
}
Following recommendations found here in StackOverflow, I tried to override the ObjectMapper using the follwoing approach, but it didn't work.
@Bean
@Primary
public ObjectMapper objectMapper(Jackson2ObjectMapperBuilder builder) {
ObjectMapper objectMapper = builder.build();
objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
JavaTimeModule javaTimeModule = new JavaTimeModule();
objectMapper.registerModule(javaTimeModule);
objectMapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"));
return objectMapper;
}