杰克逊关键解串器未被调用/识别

时间:2015-04-07 06:32:56

标签: java serialization jackson mixins

即使我实现了自定义密钥解串器,我仍然会收到此错误。在调用处理程序链期间发生以下错误:

JsonMappingException with message 'Can not find a (Map) Key deserializer for type 
[simple type, class com.abc.xyz.da.kg.types.Attribute] 
(through reference chain: com.abc.xyz.da.kg.services.models.RelationshipBean2["entities"])' 

RelationshipBean2上课

public class RelationshipBean2 {
     private ArrayList<Entity> entities;
     private String searchFilterQuery;
     private int resultCount;
     private Map<StructuralFeature, List<Object>> documentAttributeValues;
   // getters and setters

参考链 - 实体是从另一个不孕元素延伸的接口。在实现Element的类中,我有以下Map。我已经为Element编写了一个Mixin类,带有以下注释。

protected Map<Attribute, Object> attributeValues = new HashMap<Attribute, Object>();

此地图是例外的来源: -

@JsonTypeInfo(use = JsonTypeInfo.Id.CLASS, include = JsonTypeInfo.As.PROPERTY, property = "@class")

public abstract class ElementMixin implements Element {

@JsonDeserialize(keyUsing = AttributeKeyDeserializer.class)
protected Map<Attribute, Object> attributeValues = new HashMap<Attribute, Object>();
}

我已经实现了密钥反序列化器,并使用SimpleModule将其注册到ObjectMapper。

我的解串器: -

public class AttributeKeyDeserializer extends KeyDeserializer {

public AttributeKeyDeserializer() {
    super();
}


@Override
public Object deserializeKey(String content, DeserializationContext context)
            throws IOException, JsonProcessingException {
   // ObjectMapper mapper = (ObjectMapper) context.getParser().getCodec();
   // Attribute attribute = mapper.readValue(content, Attribute.class);
   // return attribute.getID();
   return "Attr";
}

ObjectMapper配置和反序列化程序注册: -

final ObjectMapper mapper = new ObjectMapper();

MixinModule mixinModule = new MixinModule("KGMixinModule", new Version(1,
            0, 0, "SNAPSHOT"));
StructuralFeatureDeserializer structuralFeatureDeserializer = new StructuralFeatureDeserializer();
mixinModule.addDeserializer(StructuralFeature.class,
            structuralFeatureDeserializer);
mixinModule.addKeyDeserializer(StructuralFeature.class,
            new StructuralFeatureKeyDeserializer());
mixinModule.addKeyDeserializer(Attribute.class,
            new AttributeKeyDeserializer());
mapper.registerModule(mixinModule);

AnnotationIntrospector primary = new JaxbAnnotationIntrospector();
AnnotationIntrospector secondary = new JacksonAnnotationIntrospector();
AnnotationIntrospector pair = new AnnotationIntrospector.Pair(primary,
            secondary);
mapper.setAnnotationIntrospector(pair);

// Set up the provider
JacksonJaxbJsonProvider jaxbProvider = new JacksonJaxbJsonProvider();
jaxbProvider.setMapper(mapper);

这里MixinModule从Jackson的SimpleModule扩展

有人可以指出我失踪的是什么,为什么没有调用解串器。我一直在苦苦挣扎。

更多背景信息:     我试图在我不拥有的API之上编写REST api,因此修改这些类超出了范围。     我只能使用Jackson v1.9.13

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

我终于发现了什么问题。我在Super class mixin中添加了带注释的Map字段,而该字段实际上存在于其中一个子类中。我假设Mixin类会在Super类中添加该字段。

最后,我为该子类创建了一个Mixin类,并在那里添加了带注释的字段,并且一切正常。