【问题标题】:Annotation not added to class level by maven-jaxb2-pluginmaven-jaxb2-plugin 未将注释添加到类级别
【发布时间】:2017-08-04 05:27:56
【问题描述】:

我正在尝试注释 jaxb 生成的 pojo,为此我使用 ma​​ven-jaxb2-plugin,我可以添加注释但不能使用默认值

我的 pom.xml 中有以下 maven-jaxb2-plugin 的配置

<plugin>
            <groupId>org.jvnet.jaxb2.maven2</groupId>
            <artifactId>maven-jaxb2-plugin</artifactId>
            <version>0.8.3</version>
            <executions>
                <execution>
                    <phase>generate-sources</phase>
                    <goals>
                        <goal>generate</goal>
                    </goals>
                </execution>
            </executions>

            <configuration>
                <extension>true</extension>
                <args>
                    <arg>-Xannotate</arg>
                </args>
                <generatePackage>com.abc.model</generatePackage>
                <schemas>
                    <schema>
                        <fileset>
                            <directory>src/main/xsd</directory>
                            <includes>
                                <include>pojo.xsd</include>
                            </includes>
                        </fileset>
                    </schema>
                </schemas>
                <bindings>
                    <binding>
                        <fileset>
                            <directory>src/main/xsd</directory>
                            <includes>
                                <include>binding.xjb</include>
                            </includes>
                        </fileset>
                    </binding>
                </bindings>
                <plugins>
                    <plugin>
                        <groupId>org.jvnet.jaxb2_commons</groupId>
                        <artifactId>jaxb2-basics</artifactId>
                        <version>0.6.4</version>
                    </plugin>
                    <plugin>
                        <groupId>org.jvnet.jaxb2_commons</groupId>
                        <artifactId>jaxb2-basics-annotate</artifactId>
                        <version>0.6.4</version>
                    </plugin>
                    <plugin>
                        <groupId>com.abc.xxy</groupId>
                        <artifactId>my-custom-annotation</artifactId>
                        <version>1.1.1</version>
                    </plugin>
                    <plugin>
                        <groupId>org.jvnet.jaxb2_commons</groupId>
                        <artifactId>jaxb2-namespace-prefix</artifactId>
                        <version>1.1</version>
                    </plugin>
                </plugins>
            </configuration>
        </plugin>

我的 xsd 和绑定如下所示:-

XSD

<xs:element name="batch" type="Batch"/>

<xs:complexType name="Batch">
    <xs:sequence>
        <xs:element name='student' type='StudentType' minOccurs='0'
                    maxOccurs='unbounded'/>
    </xs:sequence>
</xs:complexType>

<xs:complexType name="StudentType">
    <xs:sequence>
        <xs:element name="firstname" type="xs:string"/>
        <xs:element name="lastname" type="xs:string"/>
        <xs:element name="nickname" type="xs:string"/>
        <xs:element name="marks" type="xs:positiveInteger"/>
    </xs:sequence>
    <xs:attribute name='rollno' type='xs:positiveInteger'/>
</xs:complexType>

绑定

<?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"
        xsi:schemaLocation="http://java.sun.com/xml/ns/jaxb http://java.sun.com/xml/ns/jaxb/bindingschema_2_0.xsd"
        jaxb:extensionBindingPrefixes="xjc annox"
        version="2.1">

    <jaxb:bindings schemaLocation="pojo.xsd" node="/xs:schema">
        <jaxb:bindings node="xs:complexType[@name='Batch']">
            <annox:annotate target="class">
                <annox:annotate annox:class="com.abc.xyx.MyCustomAnnotation"/>
                <!--<annox:annotate target="class">com.abc.xyx.MyCustomAnnotation</annox:annotate>-->
            </annox:annotate>
        </jaxb:bindings>
        <jaxb:bindings node="xs:complexType[@name='StudentType']/xs:sequence/xs:element[@name='firstname']">
            <annox:annotate target="field">
                <annox:annotate annox:class="com.abc.xyx.MyCustomAnnotation"/>
            </annox:annotate>
        </jaxb:bindings>
        <jaxb:bindings node="xs:complexType[@name='StudentType']/xs:sequence/xs:element[@name='lastname']">
            <!--<annox:annotate target="field">@com.abc.xyx.MyCustomAnnotation(propertyAlias = "cemaforr:timestamp")</annox:annotate>-->
            <annox:annotate target="field">
                <annox:annotate annox:class="com.abc.xyx.MyCustomAnnotation"/>
            </annox:annotate>
        </jaxb:bindings>
    </jaxb:bindings>

</jaxb:bindings>

谁能告诉我哪里弄错了,提前谢谢

【问题讨论】:

    标签: java maven jaxb maven-jaxb2-plugin


    【解决方案1】:

    通过更改 binding.xml 解决了我的问题,问题是我没有将字段值传递给注释。

         <?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"
               xsi:schemaLocation="http://java.sun.com/xml/ns/jaxb http://java.sun.com/xml/ns/jaxb/bindingschema_2_0.xsd"
               jaxb:extensionBindingPrefixes="xjc annox"
               version="2.1">
    
           <jaxb:bindings schemaLocation="pojo.xsd" node="/xs:schema">
               <jaxb:bindings node="xs:complexType[@name='Batch']">
                   <annox:annotate target="class">
                       <annox:annotate annox:class="com.abc.xyx.MyCustomAnnotation"/>
                   </annox:annotate>
               </jaxb:bindings>
               <jaxb:bindings node="xs:complexType[@name='StudentType']/xs:sequence/xs:element[@name='firstname']">
                   <annox:annotate target="field">
                       <annox:annotate annox:class="com.abc.xyx.MyCustomAnnotation">
                           <annox:annotate annox:field="propertyNane">Property Value 1</annox:annotate>
                       </annox:annotate>
                   </annox:annotate>
               </jaxb:bindings>
               <jaxb:bindings node="xs:complexType[@name='StudentType']/xs:sequence/xs:element[@name='lastname']">
               <annox:annotate target="field">
                   <annox:annotate annox:class="com.abc.xyx.MyCustomAnnotation">
                       <annox:annotate annox:field="propertyNane">Property Value 2</annox:annotate>
                   </annox:annotate>
               </annox:annotate>
               </jaxb:bindings>
           </jaxb:bindings>
    
       </jaxb:bindings>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-29
      • 2013-03-31
      • 1970-01-01
      相关资源
      最近更新 更多