Java Jackson Mixin不使用Pojo到json
我的目标是xml到Pojo和Pojo到json。我已经使用jaxb对pojo编写了xml。现在我正在尝试使用jacksonjaxb将pojo转换为json。在这里,我得到了如下json,它以json格式生成JXBELENT类文件,如下所示Java Jackson Mixin不使用Pojo到json,java,json,xml,jackson,jaxb,Java,Json,Xml,Jackson,Jaxb,我的目标是xml到Pojo和Pojo到json。我已经使用jaxb对pojo编写了xml。现在我正在尝试使用jacksonjaxb将pojo转换为json。在这里,我得到了如下json,它以json格式生成JXBELENT类文件,如下所示 { "name" : "{http://xxx.xx.xx.xx.xx.xx.xx}CompositeResponse", "declaredType" : "xxx.xx.xx.xx.xx.xx.xxCompositeResponseType",
{
"name" : "{http://xxx.xx.xx.xx.xx.xx.xx}CompositeResponse",
"declaredType" : "xxx.xx.xx.xx.xx.xx.xxCompositeResponseType",
"scope" : "javax.xml.bind.JAXBElement$GlobalScope",
"value" : {
"CompositeIndividualResponse" : [ {
"ResponseMetadata" : {
"ResponseCode" : "HS000000",
"ResponseDescriptionText" : "Success"
}
} ]
},
"nil" : false,
"globalScope" : true,
"typeSubstituted" : false
}
如何删除name、declaredType、scope、nil、globalScope、types和获取以下json
{
"CompositeResponse":
{
"CompositeIndividualResponse" : [ {
"ResponseMetadata" : {
"ResponseCode" : "HS000000",
"ResponseDescriptionText" : "Success"
}
} ]
}
}
我一直在看这个,但这个对我不起作用。下面是我为jackson mixin尝试的代码
public class Main {
public static interface JAXBElementMixinT {
@JsonValue
Object getValue();
}
public static void main(String[] args) throws XMLStreamException, IOException {
ObjectMapper mapper = new ObjectMapper();
AnnotationIntrospector introspector = new JaxbAnnotationIntrospector(mapper.getTypeFactory());
mapper.setAnnotationIntrospector(introspector );
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
mapper.addMixIn(JAXBElement.class, JAXBElementMixinT.class);
String result = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(employee);
System.out.println(result);
}
}
我也尝试了以下代码,但没有运气
public abstract class JAXBElementMixIn {
@JsonIgnore abstract String getScope();
@JsonIgnore abstract boolean isNil();
@JsonIgnore abstract boolean isGlobalScope();
@JsonIgnore abstract boolean isTypeSubstituted();
@JsonIgnore abstract Class getDeclaredType();
}
有谁能帮我找出哪里错了,该怎么办,谢谢。事实上,我本周刚刚遇到了这个问题,并通过删除
AnnotationIntrospector introspector = new JaxbAnnotationIntrospector(mapper.getTypeFactory());
mapper.setAnnotationIntrospector(introspector );
一块。我还没有回顾如何将其添加回去,但这让您的其余代码能够正确地为我工作,我不再看到JAXBElement包装器。最后我能够解决这个问题。根据@Ryan answer,我不需要以下代码:
AnnotationIntrospector introspector=newjaxbannotationintrospector(mapper.getTypeFactory());
映射器setAnnotationIntrospector(内省器)代码>
但是我必须添加JaxbAnnotationModule=newjaxbannotationmodule();否则jackson将为每个元素生成链接和元数据。完整的代码如下
public class Main {
public static interface JAXBElementMixinT {
@JsonValue
Object getValue();
}
public static void main(String[] args) throws XMLStreamException, IOException {
ObjectMapper mapper = new ObjectMapper();
JaxbAnnotationModule module = new JaxbAnnotationModule();
mapper.registerModule(module)
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
mapper.addMixIn(JAXBElement.class, JAXBElementMixinT.class);
String result = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(employee);
System.out.println(result);
}
}您的解决方案几乎是正确的。我们需要下面的代码将jackson注册为jaxb模块,而不是AnnotationIntrosector。JaxbAnnotationModule=新的JaxbAnnotationModule();映射器注册表模块(模块);否则会产生一些垃圾数据。