2015-08-15 60 views
1

我在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" 
} 

zonedDateLOCALDATE的微秒的一端与,但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] 
  1. 看来的toString结果应该是在Jackson序列所期望的结果,在上面记录的打印结果,OffsetDateTime JSON节点微秒文本应以结束。
  2. 为什么format方法的结果省略了结尾?

完整的示例代码可以从我的github.com找到。

angular-springmvc-sample-boot

ISODateTest

回答

0

您的测试有2个问题:

  1. 你比较苹果和橘子,ObjectMapper序列化结果和DateTimeFormatter格式的结果。每个人都有自己的默认和配置选项,结果不一样也就不足为奇了。
  2. 100_000_000确实是1并根据ISO_LOCAL_TIME的定义,DateTimeFormatter打印出“一秒到九位数的纳秒级,按需输出多少位数”。在你的情况下,只需要1位数字,这正是你在LocalDateTime的输出中得到的。

我不完全清楚你要在这里做什么,以及你写的测试的目的是什么。如果您预期格式化程序的精度为3位数,则必须自行打印。根据您的代码中的toString格式设置是个不错的主意。

+0

感谢您的解释。我感到困惑的是为什么输出格式没有在Jackson _ObjectMapper_中统一。 – Hantsy

+0

如果通过“团结”,你的意思是如果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'。 –

+0

看来最新的2.7.2在LocalDateTime和OffsetDateTime序列化中使用了'DateTimeFormatter'。但是'ZonedDateTime'序列化不使用'DateTimeFormatter.ISO_ZONED_DATE_TIME'。 – Hantsy

3

作为一种变通方法,添加我自己的序列:

ObjectMapper objectMapper = new ObjectMapper(); 
    objectMapper.registerModule(new JavaTimeModule()); 
    SimpleModule module = new SimpleModule(); 
    module.addSerializer(ZonedDateTime.class, new JsonSerializer<ZonedDateTime>() { 
     @Override 
     public void serialize(ZonedDateTime zonedDateTime, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException, JsonProcessingException { 
      jsonGenerator.writeString(DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSZZZ").format(zonedDateTime)); 
     } 
    }); 
    objectMapper.registerModule(module); 
    objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS); 
    objectMapper.enable(SerializationFeature.INDENT_OUTPUT); 
+0

代码片段很有帮助。谢谢。 – Hantsy

0

这里是我做了配置ObjectMapper。

@Configuration 
public class JacksonConfiguration { 

    @Bean 
    public ObjectMapper objectMapper(Jackson2ObjectMapperBuilder builder) { 
    ObjectMapper objectMapper = builder.build(); 
    objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false); 
    objectMapper.configure(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS, false); 
    return objectMapper; 
    } 
}