我在Spring Boot应用程序中使用Java 8 DateTime和Jackson jsr 310支持。Java 8 DateTime序列化和杰克逊JSR 310
我禁用了SerializationFeature.WRITE_DATES_AS_TIMESTAMPS强制Jackson将localDatetime序列化为string而不是int数组。
但是我发现一个奇怪的格式问题,当日期时间微秒或nonaseconds是0。最后,我认为序列化的结果可能是等于date.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME),但它不是,格式方法省略了零。
完整的示例测试代码。
private LocalDateTime date;
private OffsetDateTime offsetDate;
private ZonedDateTime zonedDate;
@Before
public void setup() throws ServletException {
date = LocalDateTime.of(2015, 8, 15, 11, 40, 10, 100_000_000);
offsetDate = OffsetDateTime.of(2015, 8, 15, 11, 40, 10, 100_000_000, ZoneOffset.ofHours(8));
zonedDate = ZonedDateTime.of(2015, 8, 15, 11, 40, 10, 100_000_000, ZoneId.of("Asia/Shanghai"));
}
@Test
public void testDateFormat() throws Exception {
Map<String, Object> map = new HashMap<>();
map.put("localDate", date);
map.put("offsetDate", offsetDate);
map.put("zonedDate", zonedDate);
String json = objectMapper.writeValueAsString(map);
log.debug("converted json result @" + json);
JsonNode rootNode = objectMapper.readTree(json);
JsonNode localDateNode = rootNode.get("localDate");
assertEquals("local date should be equals", date.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME), localDateNode.textValue());
JsonNode offsetDateNode = rootNode.get("offsetDate");
assertEquals("offsetDate date should be equals", offsetDate.format(DateTimeFormatter.ISO_OFFSET_DATE_TIME), offsetDateNode.textValue());
JsonNode zonedDateNode = rootNode.get("zonedDate");
assertEquals("zonedDate date should be equals", zonedDate.format(DateTimeFormatter.ISO_ZONED_DATE_TIME), zonedDateNode.textValue());
}
测试失败,和日志调试输出
{
"zonedDate":"2015-08-15T11:40:10.100+08:00[Asia/Shanghai]",
"localDate":"2015-08-15T11:40:10.100",
"offsetDate":"2015-08-15T11:40:10.1+08:00"
}
zonedDate和LOCALDATE的微秒的一端与,但offsetDate不是。并且date.format也得到不同的结果,其中千分之一是不是以结尾。
格式,的toString中,JSON文本
LocalDateTime format(DateTimeFormatter.ISO_LOCAL_DATE_TIME)@2015-08-15T11:40:10.1
LocalDateTime toString @2015-08-15T11:40:10.100
LocalDateTime serialized json node text @2015-08-15T11:40:10.100
OffsetDateTime format(DateTimeFormatter.ISO_OFFSET_DATE_TIME)@2015-08-15T11:40:10.1+08:00
OffsetDateTime toString @2015-08-15T11:40:10.100+08:00
OffsetDateTime serialized json node text @2015-08-15T11:40:10.1+08:00
ZonedDateTime format(DateTimeFormatter.ISO_ZONED_DATE_TIME)@2015-08-15T11:40:10.1+08:00[Asia/Shanghai]
ZonedDateTime toString @2015-08-15T11:40:10.100+08:00[Asia/Shanghai]
ZonedDateTime serialized json node text @2015-08-15T11:40:10.100+08:00[Asia/Shanghai]
- 看来的toString结果应该是在Jackson序列所期望的结果,在上面记录的打印结果,OffsetDateTime JSON节点微秒文本应以结束。
- 为什么format方法的结果省略了结尾?
完整的示例代码可以从我的github.com找到。
感谢您的解释。我感到困惑的是为什么输出格式没有在Jackson _ObjectMapper_中统一。 – Hantsy
如果通过“团结”,你的意思是如果ObjectMapper内部使用DateTimeFormatter,它就是。请参阅[JavaTimeModule.java](https://github.com/FasterXML/jackson-datatype-jsr310/blob/master/src/main/java/com/fasterxml/jackson/datatype/jsr310/JavaTimeModule.java)。但是在Jackson中有很多配置可能会影响datetime被解析(或格式化)的方式。有关详细信息,请参阅'com.fasterxml.jackson.databind.SerializationFeature'和'com.fasterxml.jackson.databind.DeserializationFeature'。 –
看来最新的2.7.2在LocalDateTime和OffsetDateTime序列化中使用了'DateTimeFormatter'。但是'ZonedDateTime'序列化不使用'DateTimeFormatter.ISO_ZONED_DATE_TIME'。 – Hantsy