Spring   发布时间:2022-04-09  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了java – 使用JACKSON的JAXB到JSON大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

在我的应用程序中,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 Annotations(例如@XmlTransient,在我的例子中).

大佬总结

以上是大佬教程为你收集整理的java – 使用JACKSON的JAXB到JSON全部内容,希望文章能够帮你解决java – 使用JACKSON的JAXB到JSON所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。