【问题标题】:How to add include=JsonSerialize.Inclusion.NON_NULL to @JsonSerialize using annox plugin如何使用 annox 插件将 include=JsonSerialize.Inclusion.NON_NULL 添加到 @JsonSerialize
【发布时间】:2014-12-03 15:53:27
【问题描述】:

我们使用 maven 插件 maven-jaxb2-plugin 从 xsd 生成 JAXB 对象。以下是我们拥有的依赖项

jaxb2 基础 - 0.6.2
jaxb2-basics-annotate - 0.6.2

在我们的 maven 文件中,我们还包含了 -Xannotate 和 -XtoString

    <plugin>                
<groupId>org.jvnet.jaxb2.maven2</groupId>
                    <artifactId>maven-jaxb2-plugin</artifactId>
                    <executions>
                         <execution>
                            <id>exec1</id>
                            <phase>generate-sources</phase>
                            <goals>
                                <goal>generate</goal>
                            </goals>
                            <configuration>
                                <schemaDirectory>${basedir}/src/main/resources/xsd</schemaDirectory>
                                <bindingDirectory>${basedir}/src/main/resources/xsd</bindingDirectory>
                                <generatePackage>org.learning.json.generated</generatePackage>
                                <generateDirectory>${basedir}/generated</generateDirectory>
                                <clearOutputDir>false</clearOutputDir>
                                <includeSchemas>
                                    <includeSchema>Person.xsd</includeSchema>
                                </includeSchemas>
                                <plugins>
                                    <plugin>
                                        <groupId>org.jvnet.jaxb2_commons</groupId>
                                        <artifactId>jaxb2-basics</artifactId>
                                        <version>0.6.2</version>
                                    </plugin>
                                    <plugin>
                                        <groupId>org.jvnet.jaxb2_commons</groupId>
                                        <artifactId>jaxb2-basics-annotate</artifactId>
                                        <version>0.6.2</version>
                                    </plugin>
                                </plugins>
                                <args>
                                    <arg>-Xannotate</arg>
                                    <arg>-XtoString</arg>
                                </args>
                            </configuration>
                        </execution>

绑定文件如下

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<jaxb:bindings xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
               xmlns:xs="http://www.w3.org/2001/XMLSchema" 
               xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
               xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
               xmlns:annox="http://annox.dev.java.net"
               jaxb:extensionBindingPrefixes="xjc annox"
               jaxb:version="2.0">

     <jaxb:bindings schemaLocation="Person.xsd" multiple="true">  
        <jaxb:bindings node="xs:complexType[@name='personType']/xs:sequence/xs:element[@type='xs:date']" multiple="true">
            <annox:annotate>
                <annox:annotate target="getter" annox:class="org.codehaus.jackson.map.annotate.JsonSerialize" 
                                using="org.learning.json.JsonDateSerializer"/>
            </annox:annotate>
        </jaxb:bindings>
     </jaxb:bindings>
</jaxb:bindings>

这确实添加了@JsonSerialize(使用=JsonDateSerializer.class)。但我尝试了以下几个选项来添加 include=JsonSerialize.Inclusion.NON_NULL,但是没有用

<annox:annotate>
                <annox:annotate target="getter" annox:class="org.codehaus.jackson.map.annotate.JsonSerialize" 
                                using="org.learning.json.JsonDateSerializer"
        include="org.codehause.jackson.map.annotate.JsonSerialize.Inclusion.NON_NULL"/>
            </annox:annotate> 



<annox:annotate>
                <annox:annotate target="getter" annox:class="org.codehaus.jackson.map.annotate.JsonSerialize" 
                                using="org.learning.json.JsonDateSerializer"
        include="org.codehause.jackson.map.annotate.JsonSerialize$Inclusion.NON_NULL"/>
            </annox:annotate> 

但在所有情况下,都会出现 ValueParseException。那么将JsonSerialize的include(),typing()等参数添加到注释中的正确方法是什么。

另外,基于How to add Jackson annotations to POJO generated from XSD by JAXB/XJC? 我也试过了

<jaxb:bindings schemaLocation="Person.xsd" multiple="true">  
        <jaxb:bindings node="xs:complexType[@name='personType']/xs:sequence/xs:element[@type='xs:date']" multiple="true">
            <annox:annotate>
                <annox:annotate target="getter" annox:class="org.codehaus.jackson.map.annotate.JsonSerialize" 
                                using="org.learning.json.JsonDateSerializer"/>
            </annox:annotate>
            <annox:annotate>
                    @org.codehaus.jackson.map.annotate.JsonSerialize
                    (include=org.codehaus.jackson.map.annotate.JsonSerialize.Inclusion.ALWAYS
            </annox:annotate>
        </jaxb:bindings>
     </jaxb:bindings>

这也没有在注释中添加任何包含部分。

【问题讨论】:

    标签: json maven-jaxb2-plugin annox jaxb2-annotate-plugin


    【解决方案1】:

    免责声明:我是jaxb2-annotate-plugin的作者。

    首先,试试这个XML语法(从1.0.0开始不推荐使用):

    <annox:annotate
        target="getter"
        annox:class="org.codehaus.jackson.map.annotate.JsonSerialize" 
        using="org.learning.json.JsonDateSerializer"
        include="NON_NULL"/>
    

    接下来,试试这个 Java 语法:

    <annox:annotate>
        @org.codehaus.jackson.map.annotate.JsonSerialize
            (include=org.codehaus.jackson.map.annotate.JsonSerialize.Inclusion.ALWAYS)
    </annox:annotate>
    

    您链接到的答案有错字 - 最后一个 ) 丢失了。也许这就是问题所在,也许不是。

    我认为这应该可行。如果没有,请在tests 中向我发送带有示例项目的拉取请求。我会让它工作的。

    注意:您必须使用 jaxb2-annotate-plugin 1.0.0 或更高版本(当前为 1.0.1)才能使用 Java 语法。

    【讨论】:

    • 您提到的 XML 语法非常有效。再次感谢您。
    猜你喜欢
    • 2013-04-09
    • 1970-01-01
    • 1970-01-01
    • 2021-10-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多