如何通过JAXB / XJC向XSD生成的POJO添加Jackson注释? [英] How to add Jackson annotations to POJO generated from XSD by JAXB/XJC?

查看:101
本文介绍了如何通过JAXB / XJC向XSD生成的POJO添加Jackson注释?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用JAXB从XSD生成POJO。我正在使用Jersey / Jackson将POJO序列化为JSON。对于整个项目,我创建了一个具有 Inclusion.NON_NULL set的ObjectMapper。但对于一个特定的序列化类,我想包含空值。我知道,我必须在POJO上使用 Inclusion.ALWAYS 来覆盖ObjectMapper,但我使用的POJO是从XSD生成的。

I'm generating POJOs from XSDs using JAXB. And I'm using Jersey/Jackson to serialize the POJOs to JSONs. For the whole project, I've created an ObjectMapper that has Inclusion.NON_NULL set. But for one particular serialization class, I want to include null values. I know, I have to use Inclusion.ALWAYS on the POJO to override the ObjectMapper, but the POJO I'm using is generated from XSDs.

有没有办法将杰克逊的注释 @JsonSerialize(include = Inclusion.ALWAYS)添加到该特定的POJO在编组期间?

Is there a way to add this Jackson's annotation @JsonSerialize(include=Inclusion.ALWAYS) to that one particular POJO during marshalling?

http://pastebin.com/a2Gvw19U

推荐答案

如果您只想添加一个注释,请考虑使用我的 JAXB2 Annotate Plugin 。例如,参见这个例子,特别是此绑定文件

If you just want to add one annotation, consider using my JAXB2 Annotate Plugin. See, for instance, this example, specifically this bindings file.

在你的情况下,绑定看起来像:

In your case the bindings will look something like:

<jaxb:bindings node="xs:complexType[@name='myPOJO']">
    <annox:annotate>
        @org.codehaus.jackson.map.annotate.JsonSerialize
            (include=org.codehaus.jackson.map.annotate.JsonSerialize.Inclusion.ALWAYS)
    </annox:annotate>
</jaxb:bindings>

(未经测试,只是草图。)

(Not tested, just a sketch.)

几点提示:


  • 语法是Java,但你必须使用完全限定的类名。

  • Jackson JAR必须出现在XJC类路径中,否则将无法找到您的注释。

SO免责声明:我是提到的插件的作者。

SO disclaimer: I am the author of the mentioned plugin.

这篇关于如何通过JAXB / XJC向XSD生成的POJO添加Jackson注释?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆