简体   繁体   English

使用JACKSON的JAXB到JSON

[英]JAXB to JSON using JACKSON

In my application the JAXB output generates like: 在我的应用程序中,JAXB输出生成如下:

this.marshalOut(jaxb_Object, fileOutputStream);

this is method call to the spring Object XML Mapping Marshallers that generate XML files. 这是对生成XML文件的spring Object XML Mapping Marshallers的方法调用。 Now, I also like to generate JSON files after this line. 现在,我也喜欢在这一行之后生成JSON文件。 Any one have idea about generating JSON output using JAXB input. 任何人都有关于使用JAXB输入生成JSON输出的想法。

I found this example code online: 我在网上找到了这个示例代码:

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);

The setAnnotationIntrospector is deprecated, is there any other way of solving this problem? 不推荐使用setAnnotationIntrospector ,有没有其他方法可以解决这个问题?

The following works (and does not use any deprecated constructors) : 以下工作(并且不使用任何已弃用的构造函数):

ObjectMapper mapper = new ObjectMapper();

AnnotationIntrospector introspector =
    new JaxbAnnotationIntrospector(mapper.getTypeFactory());   

mapper.setAnnotationIntrospector(introspector);

Specifically, this line 具体来说,这一行

new JaxbAnnotationIntrospector(mapper.getTypeFactory());

uses a non-deprecated constructor. 使用不推荐的构造函数。 I've tested this and it successfully processes JAXB Annotations (such as @XmlTransient, in my case). 我已经测试了它并成功处理了JAXB Annotations(例如@XmlTransient,在我的例子中)。

You can use jackson-module-jaxb-annotations as stated in the doc you can register a JaxbAnnotationModule module: 您可以使用jackson-module-jaxb-annotations,如文档中所述,您可以注册JaxbAnnotationModule模块:

JaxbAnnotationModule module = new JaxbAnnotationModule();
// configure as necessary
objectMapper.registerModule(module);

Doing so you can now use both JAXB annotation and Jackson native annotation. 这样,您现在可以使用JAXB注释和Jackson本机注释。

The correct solution for me was: 对我来说,正确的解决方案是:

ObjectMapper objectMapper = new ObjectMapper();
objectMapper.setAnnotationIntrospector(new JaxbAnnotationIntrospector());

According to the Jackson javadoc: 根据Jackson javadoc:

setAnnotationIntrospector

@Deprecated
public final void setAnnotationIntrospector(AnnotationIntrospector ai)

    Deprecated. Since 1.8, use either withAnnotationIntrospector(AnnotationIntrospector) or Module API instead

    Method for replacing existing annotation introspector(s) with specified introspector. Since this method modifies state of configuration object directly, its use is not recommended

Did you check the method withAnnotationIntrospector(AnnotationIntrospector ai) to see wheter or not it's useful in your case? 您是否检查过withAnnotationIntrospector(AnnotationIntrospector ai)以查看它是否对您的案例有用?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM