【发布时间】:2026-02-04 17:50:01
【问题描述】:
我有一个这样的模型:
public class Type {
@XmlElementRef(name = "ConversationId", namespace = "...", type = JAXBElement.class, required = false)
protected JAXBElement<String> conversationId;
}
和json的一部分:
"conversationId": {
"declaredType": "java.lang.String",
"globalScope": false,
...,
"value": "ABC000000001"
},
我尝试使用此映射器进行反序列化:
public static interface JAXBElementMixin {
@JsonValue
Object getValue();
}
ObjectMapper mapper = new ObjectMapper();
JaxbAnnotationIntrospector jaxbAnnotationIntrospector=new JaxbAnnotationIntrospector(mapper.getTypeFactory());
JacksonAnnotationIntrospector jacksonAnnotationIntrospector=new JacksonAnnotationIntrospector();
mapper.setAnnotationIntrospector(AnnotationIntrospector.pair(jaxbAnnotationIntrospector, jacksonAnnotationIntrospector));
mapper.addMixIn(JAXBElement.class, JAXBElementMixin.class);
但无论如何,我的程序运行不佳:
com.fasterxml.jackson.databind.JsonMappingException:没有找到适合类型[简单类型,类 javax.xml.bind.JAXBElement] 的构造函数:无法从 JSON 对象实例化(缺少默认构造函数或创建者,或者可能需要添加/启用类型信息?)
我想阅读一些关于 mixins 的文档,但是 jackson 的官方网站上有过时的信息,我使用的是 2.5.1 版本
【问题讨论】:
-
我也有同样的问题,找到解决办法了吗?
标签: java json serialization jaxb jackson