Spring REST服务将日期时间转换为数字



我在swagger中构建了一个openapi模式,该模式的字段类型为string,格式为:date-time。在示例窗口中,它向我显示

"报告日期时间":"2022-02-02T10:56:33.310Z";

。但当我打电话给我的服务时,它会回复

"报告日期时间":1639746778.200000000

我使用openapi生成器maven插件5.1.0版本生成spring-api类。生成的响应类具有以下字段

@JsonProperty("reportingDateTime"(
@org.springframework.format.annotation.DateTimeFormat(iso=org.springfframework.format.annotation.DateTimeFormat.iso.DATE_TIME(
private偏移日期时间报告日期时间;

为了获得正确的响应类型,应该进行什么更改?

Jackson的默认设置包括将日期格式化为时间戳。这就是你在这里看到的。

将以下内容添加到您的应用程序属性以关闭此功能:

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

我认为使用DateTimeFormat进行json响应可能是错误的。

试着阅读这篇文章-https://www.baeldung.com/jackson-serialize-dates#java-8

最新更新