如何使用 annox 插件将 include=JsonSerialize.Inclusion.NON_NULL 添加到@


我们正在使用 maven plugin

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

JAXB2 基础知识 - 0.6.2
JAXB2-基础知识-注释 - 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(using=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()、typeping() 等参数添加到注释的正确方法是什么?

另外,基于如何将杰克逊注释添加到由 JAXB/XJC 从 XSD 生成的 POJO 中?我也试过

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

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

免责声明:我是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>

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

我认为这应该有效。如果没有,请向我发送一个拉取请求,其中包含测试中的示例项目。我会让它工作。

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

最新更新