Java 找不到类型 [简单类型,类 com.comcast.ivr.core.domain.AutoHandlingSlotKey] 的(映射)密钥解串器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6371092/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-16 05:25:05  来源:igfitidea点击:

Can not find a (Map) Key deserializer for type [simple type, class com.comcast.ivr.core.domain.AutoHandlingSlotKey]

javajsonHymansondeserialization

提问by Mick Knutson

I have a domain object that has a Map:

我有一个具有 Map 的域对象:

private Map<AutoHandlingSlotKey, LinkedHashSet<AutoFunction>> autoHandling;

When I serialize the object, I get this:

当我序列化对象时,我得到了这个:

"autoHandling" : [ "java.util.HashMap", {
} ],

This Map's key is a custom Object:

这个 Map 的键是一个自定义对象:

public class AutoHandlingSlotKey implements Serializable {
    private FunctionalArea slot; // ENUM
    private String returnView;   // ENUM

So, I am not sure how to correct this issue I keep getting when I deserialize the object:

因此,我不确定如何纠正反序列化对象时不断遇到的这个问题:

org.codehaus.Hymanson.map.JsonMappingException: Can not find a (Map) Key deserializer for type [simple type, class com.comcast.ivr.core.domain.AutoHandlingSlotKey]

Can someone help me to understand how to correct this issue if I do NOThave access to the domain object to modify?

有人能帮我可以了解如何解决此问题,如果我能够访问域对象要修改?

采纳答案by StaxMan

By default, Hymanson tries to serialize Java Maps as JSON Objects (key/value pairs), so Map key object must be somehow serialized as a String; and there must be matching (and registered) key deserializer. Default configuration only supports a small set of JDK types (String, numbers, enum). So mapper has no idea as to how to take a String and create AutoHandlingSlotKey out of it. (in fact I am surprised that serializer did not fail for same reason)

默认情况下,Hymanson 尝试将 Java Maps 序列化为 JSON 对象(键/值对),因此 Map 键对象必须以某种方式序列化为 String;并且必须有匹配(和注册)的密钥解串器。默认配置仅支持一小组 JDK 类型(字符串、数字、枚举)。所以映射器不知道如何获取一个字符串并从中创建 AutoHandlingSlotKey。(事实上​​,我很惊讶序列化程序并没有因为同样的原因而失败)

Two obvious ways to solve this are:

解决此问题的两种明显方法是:

  • Implement and register a "key deserializer"
  • Implement and register a custom deserializer for Maps.
  • 实现并注册一个“key deserializer”
  • 为 Maps 实现并注册自定义反序列化器。

In your case it is probably easier to do former. You may also want to implement custom key serializer, to ensure keys are serializer in proper format.

在你的情况下,做前者可能更容易。您可能还想实现自定义密钥序列化程序,以确保密钥是正确格式的序列化程序。

The easiest way to register serializers and deserializers is by Module interfacethat was added in Hymanson 1.7 (and extended in 1.8 to support key serializers/deserializers).

注册序列化器和反序列化器的最简单方法是通过在 Hymanson 1.7 中添加的Module 接口(并在 1.8 中扩展以支持关键序列化器/反序列化器)。

回答by estebanrv

This was asked a long time ago, and is the first google result when looking up the error, but the accepted answer has no code and might be confusing for a Hymanson beginner (me). I eventually found this answerthat helped.

这是很久以前问过的,是查找错误时的第一个谷歌结果,但接受的答案没有代码,可能会让Hyman逊初学者(我)感到困惑。我最终找到了这个有帮助的答案

So, as stated in accepted answer, Implementing and register a "key deserializer" is the way to go. You can do this like this.

因此,如已接受的答案中所述,实施和注册“密钥解串器”是要走的路。你可以这样做。

SimpleModule simpleModule = new SimpleModule();
simpleModule.addKeyDeserializer(YourClass.class, new YourClassKeyDeserializer());
objectMapper.registerModule(simpleModule);

And for the class, all you have to do is:

而对于课堂,你所要做的就是:

class YourClassKeyDeserializer extends KeyDeserializer
{
    @Override
    public Object deserializeKey(final String key, final DeserializationContext ctxt ) throws IOException, JsonProcessingException
    {
        return null; // replace null with your logic
    }
}

That's it! No annotation on classes, not custom deserializer for maps, etc.

就是这样!类上没有注释,地图上没有自定义反序列化器等。