gpt4 book ai didi

java - 找不到类型 [简单类型,类 ...] 的(映射) key 反序列化器

转载 作者:IT老高 更新时间:2023-10-28 12:47:19 28 4
gpt4 key购买 nike

我有一个包含 map 的域对象:

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

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

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

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

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

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

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

如何解决这个问题?我确实没有有权访问要修改的域对象。

最佳答案

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

因此,正如接受的答案中所述,实现和注册“ key 反序列化器”是要走的路。你可以这样做。

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

对于类(class),你所要做的就是:

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

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

关于java - 找不到类型 [简单类型,类 ...] 的(映射) key 反序列化器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6371092/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com