'Spring REST Service converts date-time to number
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?
Solution 1:[1]
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
Solution 2:[2]
I think using DateTimeFormat for json response may be wrong.
Try to read this article - https://www.baeldung.com/jackson-serialize-dates#java-8
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Rob Spoor |
| Solution 2 | dimamj |
