在我的应用程序中,JAXB 输出生成如下:
this.marshalOut(jaxb_Object, fileOutputStream);
这是对生成 XML 文件的 spring Object XML Mapping Marshallers 的方法调用。现在,我也喜欢在这一行之后生成 JSON 文件。任何人都知道使用 JAXB 输入生成 JSON 输出。
我在网上找到了这个示例代码:
ObjectMapper mapper = new ObjectMapper();
AnnotationIntrospector introspector = new JacksonAnnotationIntrospector();
// make deserializer use JAXB annotations (only)
mapper.getDeserializationConfig().setAnnotationIntrospector(introspector);
// make serializer use JAXB annotations (only)
mapper.getSerializationConfig().setAnnotationIntrospector(introspector);
mapper.writeValue( outputStream, jaxb_object);
setAnnotationIntrospector
已弃用,还有其他方法可以解决这个问题吗?
最佳答案
以下工作(并且不使用任何已弃用的构造函数):
ObjectMapper mapper = new ObjectMapper();
AnnotationIntrospector introspector =
new JaxbAnnotationIntrospector(mapper.getTypeFactory());
mapper.setAnnotationIntrospector(introspector);
具体来说,这一行
new JaxbAnnotationIntrospector(mapper.getTypeFactory());
使用了一个未弃用的构造函数。我已经对此进行了测试,它成功地处理了 JAXB 注释(例如在我的例子中为 @XmlTransient)。
关于java - 使用 JACKSON 将 JAXB 转换为 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11441299/