找不到类型 [simple type, class ...] 的 (Map) Key 反序列化器 [英] Can not find a (Map) Key deserializer for type [simple type, class ...]

查看:24
本文介绍了找不到类型 [simple type, class ...] 的 (Map) Key 反序列化器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a domain object that has a Map:

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

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

When I serialize the object, I get this:

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

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

This Map's key is a custom Object:

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

因此,我不确定如何纠正反序列化对象时不断出现的异常:

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

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

如何解决这个问题?我无权访问要修改的域对象.

How to correct this issue? I do not have access to the domain object to modify.

推荐答案

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

By default, Jackson 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)

解决这个问题的两个明显方法是:

Two obvious ways to solve this are:

  • 实现并注册一个key deserializer"
  • 为地图实现并注册自定义反序列化器.

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

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.

注册序列化器和反序列化器的最简单方法是通过在 Jackson 1.7(和在 1.8 中扩展以支持关键序列化器/反序列化器).

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

这篇关于找不到类型 [simple type, class ...] 的 (Map) Key 反序列化器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆