0

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

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

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

4

1 回答 1

0

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

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

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

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

于 2014-12-03T07:53:27.540 回答