英文:
Cannot find a (Map) Key deserializer for type [simple type, class java.time.OffsetDateTime
问题
我有一个无法更改的类,其格式如下:
import java.time.OffsetDateTime;
public class MyClass {
private OffsetDateTime updateDate;
...
}
我将以json格式接收这些数据,并尝试映射回我需要的类:
import com.fasterxml.jackson.databind.ObjectMapper;
...
ObjectMapper mapper = new ObjectMapper();
MyClass myObj = mapper.readValue(jsonData, MyClass.class);
但我得到以下错误:
无法找到类型为[简单类型,类java.time.OffsetDateTime]的(Map)键反序列化程序
在这个主类中,我还有子类,也返回一个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
}
}
我能在这里得到一些帮助吗? :)
谢谢!
英文:
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!
答案1
得分: 1
我会努力说服对方改变他们的格式(任何人都应该明显地看到当前的格式存在很多冗余并且使用起来很困难),并将日期发送为 ISO-8601 格式的字符串,例如。这样,您可以使用 Jackson-Datatype-JSR310 模块来反序列化您的对象。
如果无法对格式进行更改,那么您将不得不为其编写自己的自定义反序列化器。
英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论