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

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

问题描述

我有一个具有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", {
} ],

此地图的密钥是自定义对象:

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 key对象必须以某种方式序列化为String;并且必须有匹配的(和注册的)密钥反序列化器.默认配置仅支持一小部分JDK类型(字符串,数字,枚举).因此,映射器不知道如何获取String并从中创建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:

  • 实施并注册密钥解串器"
  • 为地图实现并注册自定义反序列化器.

就您而言,做前者可能更容易.您可能还需要实现自定义密钥序列化程序,以确保密钥是正确格式的序列化程序.

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).

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

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