java 使用 JACKSON 将 JAXB 转换为 JSON

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11441299/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 05:08:36  来源:igfitidea点击:

JAXB to JSON using HymanSON

javajsonspringjaxb

提问by user1417746

In my application the JAXB output generates like:

在我的应用程序中,JAXB 输出生成如下:

this.marshalOut(jaxb_Object, fileOutputStream);

this.marshalOut(jaxb_Object, fileOutputStream);

this is method call to the spring Object XML Mapping Marshallers that generate XML files. Now, I also like to generate JSON files after this line. Any one have idea about generating JSON output using JAXB input.

这是对生成 XML 文件的 spring 对象 XML 映射编组器的方法调用。现在,我也喜欢在这一行之后生成 JSON 文件。任何人都知道使用 JAXB 输入生成 JSON 输出。

I found this example code online:

我在网上找到了这个示例代码:

ObjectMapper mapper = new ObjectMapper();
AnnotationIntrospector introspector = new HymansonAnnotationIntrospector();
// 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 setAnnotationIntrospectoris deprecated, is there any other way of solving this problem?

setAnnotationIntrospector已过时,是否有解决这个问题的任何其他方式?

回答by Michael Christoff

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 注释(例如@XmlTransient,在我的情况下)。

回答by landal79

You can use Hymanson-module-jaxb-annotationsas stated in the doc you can register a JaxbAnnotationModulemodule:

您可以使用文档中所述的Hymanson-module-jaxb-annotations您可以注册一个JaxbAnnotationModule模块:

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

Doing so you can now use both JAXB annotation and Hymanson native annotation.

这样做您现在可以同时使用 JAXB 注释和 Hymanson 原生注释。

回答by McIntosh

The correct solution for me was:

对我来说正确的解决方案是:

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

回答by posdef

According to the Hymanson javadoc:

根据Hyman逊 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)以查看它对您的情况是否有用?