无法找到类型为 [简单类型,类 java.time.OffsetDateTime] 的(Map)键解串器。

huangapple go评论73阅读模式
英文:

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? 无法找到类型为 [简单类型,类 java.time.OffsetDateTime] 的(Map)键解串器。

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.

huangapple
  • 本文由 发表于 2020年10月9日 00:26:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/64266769.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定