简体   繁体   中英

Cannot find a (Map) Key deserializer for type [simple type, class java.time.OffsetDateTime

I have a class that I cannot change with the following format:

import java.time.OffsetDateTime;
public class MyClass {
    private OffsetDateTime updateDate;
    ...
}

I receive this data as json and I try to map back to the class I need:

import com.fasterxml.jackson.databind.ObjectMapper;
...
ObjectMapper mapper = new ObjectMapper();
MyClass myObj = mapper.readValue(jsonData, MyClass.class);

And I get the following:

Cannot find a (Map) Key deserializer for type [simple type, class java.time.OffsetDateTime

I also have sub-classes in this main class that also return an OffsetDateTime.

{
  "updateDate":{
  "offset":{
     "totalSeconds":0,
     "id":"Z",
     "rules":{
        "transitions":[
           
        ],
        "transitionRules":[
           
        ],
        "fixedOffset":true
     }
  },
  "nano":935767000,
  "year":2020,
  "monthValue":10,
  "dayOfMonth":8,
  "hour":15,
  "minute":33,
  "second":0,
  "month":"OCTOBER",
  "dayOfWeek":"THURSDAY",
  "dayOfYear":282
}
}

Can I get some help here? :)

Thank you!

I would try really hard to convince the other side to change their format (it should be obvious to anyone that the current one has a lot of redundancy and is hard to use), and send the date as ISO-8601 string, for example. This way you could use Jackson-Datatype-JSR310 module to deserialize your object.

If it's not possible to make changes to the format, them you'll have to write your own custom deserializer for it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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