【发布时间】:2017-12-21 22:20:06
【问题描述】:
使用返回 JSON 有效负载的 Spring Boot 创建 REST 服务时,Java 8 的 LocalDateTime 被序列化。格式字符串(即模式)在哪里定义(大概)Jackson 在将对象编组为字符串时应用,我在哪里可以找到 Spring Boot 中应用该格式的默认配置(即设置格式化程序)?
(注意:我可以在有效负载中看到格式化的日期,而不是时间戳。)
【问题讨论】:
-
LocalDateTime 不是时间戳,所以看不到时间戳是很正常的。也就是说,文档回答了您的问题:docs.spring.io/spring-boot/docs/current/reference/htmlsingle/…
-
文档将我指向
Jackson2ObjectMapperBuilder,而后者又指向JSR310Module并通过... -
...
LocalDateTimeSerializer最后到LocalDateTime::toString。我的LocalDateTime设置为1776-07-04T12:29Z,但我得到1776-07-04T12:29:00Z。文档说“使用的格式将是最短的......其中省略的部分暗示为零”:uuuu-MM-dd'T'HH:mm。为什么我会收到:00回复? -
LocalDateTime 没有时区。所以你不能有这样一个最终的 Z。如果你有,你可能实际上并没有序列化 LocalDateTime。
System.out.println(LocalDateTime.parse("1776-07-04T12:29"));产生您期望的输出:1776-07-04T12:29
标签: spring spring-mvc spring-boot jackson