Deserialize “Zulu” time in ISO8601 format in jackson

霸气de小男生 提交于 2019-12-10 18:16:36

问题


I have a need to de-serialize time of format 2016-11-28T10:34:25.097Z using Jackson into ZonedDateTime of Java8.

I believe I correctly configured ObjectMapper (a factory method):

 @Bean
ObjectMapper getObjectMapper() {
    ObjectMapper objectMapper = new ObjectMapper();
    // some other config...
    objectMapper.registerModule(new JavaTimeModule());
    return objectMapper;
}

And I have in my code for DTO a field

  @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd'T'HH:mm:ss.SSSZ")
private ZonedDateTime updatedAt;

when I try to parse this by Jackson, I get

 java.lang.IllegalArgumentException: Can not deserialize value of type java.time.ZonedDateTime 
 from String "2016-11-28T10:34:25.097Z": Text '2016-11-28T10:34:25.097Z' could not be parsed,
 unparsed text found at index 23  at [Source: N/A; line: -1, column: -1]  

Without @JsonFormat problem remains.

How I could possibly overcome this?


回答1:


The problem is probably with 'Z' in the pattern. It does not allow literal 'Z' in the date time value. Try 'X' instead.

  @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd'T'HH:mm:ss.SSSX")



回答2:


In my optinion the following JsonFormat for ISO 8601

@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd'T'HH:mm:ss.SSSXXX")

is much better, since this format is more intuitive to read and allows timezones like ACST with UTC offset of +09:30 too.



来源:https://stackoverflow.com/questions/40845843/deserialize-zulu-time-in-iso8601-format-in-jackson

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!