Home > Enterprise >  Spring REST Service converts date-time to number
Spring REST Service converts date-time to number

Time:02-02

I have build an openapi schema in swagger with an field of type string and format: date-time. In the example window it shows me

"reportingDateTime": "2022-02-02T10:56:33.310Z"

. But when I call my service it responses

"reportingDateTime": 1639746778.200000000

I generate the spring api classes with the openapi-generator-maven-plugin version 5.1.0. The resulting response class has this field

@JsonProperty("reportingDateTime")
@org.springframework.format.annotation.DateTimeFormat(iso = org.springframework.format.annotation.DateTimeFormat.ISO.DATE_TIME)
private OffsetDateTime reportingDateTime;

What shall a change to get the correct response type?

CodePudding user response:

Jackson's default settings include formatting dates to timestamps. That's what you're seeing here.

Add the following to your application properties to turn this off:

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

CodePudding user response:

I think using DateTimeFormat for json response may be wrong.

Try to read this article - https://www.baeldung.com/jackson-serialize-dates#java-8

  •  Tags:  
  • Related