简体   繁体   English

将毫秒时间戳反序列化为java.time.Instant

[英]Deserialize millisecond timestamp to java.time.Instant

I'm attempting to read a JSON file using Jackson and store one of the fields that is stored as a epoch milliseconds as a Java Instant , however deserialization is not behaving as expected. 我正在尝试使用Jackson读取JSON文件,并将其中一个字段存储为纪元毫秒作为Java Instant ,但反序列化的行为不符合预期。

Here is what I am seeing when trying to read the timestamp: 以下是我在尝试阅读时间戳时看到的内容:

1503115200000 1503115200000

Jackson is setting the Instant field as +49601-10-28T16:00:00Z . 杰克逊将Instant字段设置为+49601-10-28T16:00:00Z

This appears to be occurring because Jackson's default is to read the timestamp with Instant.ofEpochSecond(Long l) instead of Instant.ofEpochMilli(Long l) . 这似乎是因为Jackson的默认值是使用Instant.ofEpochSecond(Long l)而不是Instant.ofEpochMilli(Long l)来读取时间戳。

Is there a way to set the Jackson ObjectMapper to use the ofEpochMilli method instead? 有没有办法将Jackson ObjectMapper设置为使用ofEpochMilli方法? This is what I currently have for my ObjectMapper : 这是我目前对我的ObjectMapper

ObjectMapper om = new ObjectMapper()
            .registerModule(new JavaTimeModule())
            .configure(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS, false)
            .configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false)
            .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
            .setSerializationInclusion(Include.NON_NULL);

Note 注意

If I change the input JSON to ISO date such as 2017-08-19T04:00:00Z or to epoch seconds such as 1503115200 the Instant field is able to set properly. 如果我将输入JSON更改为ISO日期(例如2017-08-19T04:00:00Z或更改为纪元秒(例如1503115200Instant字段可以正确设置。

Unfortunately the JSON input must be epoch milliseconds eg 1503115200000 . 不幸的是,JSON输入必须是纪元毫秒,例如1503115200000

Solution was to add .configure(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS, false) to the ObjectMapper. 解决方案是将.configure(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS, false)到ObjectMapper。 Complete ObjectMapper looks like: 完整的ObjectMapper看起来像:

ObjectMapper om = new ObjectMapper()
            .registerModule(new JavaTimeModule())
            .configure(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS, false)
            .configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false)
            .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
            .configure(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS, false)
            .setSerializationInclusion(Include.NON_NULL);

From https://github.com/FasterXML/jackson-modules-java8/blob/master/datetime/src/main/java/com/fasterxml/jackson/datatype/jsr310/JavaTimeModule.java : 来自https://github.com/FasterXML/jackson-modules-java8/blob/master/datetime/src/main/java/com/fasterxml/jackson/datatype/jsr310/JavaTimeModule.java

The more ambiguous integer types are read as fractional seconds without a decimal point if {@code READ_DATE_TIMESTAMPS_AS_NANOSECONDS} is enabled (it is by default), and otherwise they are read as milliseconds. 如果启用{@code READ_DATE_TIMESTAMPS_AS_NANOSECONDS}(默认情况下),则更不明确的整数类型将被读取为没有小数点的小数秒,否则它们将被读取为毫秒。

So you need to disable READ_DATE_TIMESTAMPS_AS_NANOSECONDS. 因此,您需要禁用READ_DATE_TIMESTAMPS_AS_NANOSECONDS。

For those who use Spring Boot and experience this issue during rest request deserialization - provide this in the application.properties: 对于那些使用Spring Boot并在休息请求反序列化期间遇到此问题的人 - 请在application.properties中提供:

spring.jackson.deserialization.read-date-timestamps-as-nanoseconds=false

@JB Nizet answer is correct, but since Spring Boot has it's own ObjectMapper inside - you need to configure this value in the properties. @JB Nizet的答案是正确的,但由于Spring Boot里面有自己的ObjectMapper,你需要在属性中配置这个值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM