java 在jaxb 2-maven-plugin生成的类中需要@XmlRootElement

ufj5ltwl  于 2023-06-04  发布在  Java
关注(0)|答案(2)|浏览(241)

我有一组从WSDL生成的Java类,它们工作得很好;我正在为我正在使用的另一个Web服务向项目中添加另一个WSDL,但是在从第二个WSDL生成的类中没有得到@XmlRootElement注解,我不明白为什么没有。
下面是pom的插件部分:

<plugin>
          <groupId>org.codehaus.mojo</groupId>
          <artifactId>jaxb2-maven-plugin</artifactId>
          <version>2.2</version>
          <executions>
              <execution>
                  <id>xjc</id>
                  <goals>
                      <goal>xjc</goal>
                  </goals>
              </execution>
          </executions>
          <configuration>
              <sourceType>wsdl</sourceType>
              <sources>
                  <source>${resources.path}my-module/src/main/resources/wsdl/w1.wsdl</source>
                  <source>${resources.path}my-module/src/main/resources/wsdl/w2.wsdl</source>
              </sources>
              <extension>true</extension>
              <xjbSources>
                  <xjbSource>src/main/resources/xjb/bindings.xjb</xjbSource>
              </xjbSources>
          </configuration>
        </plugin>

这里是绑定。xjb:

<?xml version="1.0"?>
<jxb:bindings version="1.0" xmlns:jxb="http://java.sun.com/xml/ns/jaxb"
              xmlns:xjc= "http://java.sun.com/xml/ns/jaxb/xjc"
              jxb:extensionBindingPrefixes="xjc" xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <jxb:bindings>
        <!-- annotate tag here? -->
        <jxb:globalBindings>
            <xjc:simple/>
        </jxb:globalBindings>
    </jxb:bindings>
</jxb:bindings>

我在this SO post中读到了关于使用annotate标签的内容,所以我插入了

<annox:annotate>
            <annox:annotate annox:class="javax.xml.bind.annotation.XmlRootElement" />
        </annox:annotate>

在bindings.xjb文件中指定的位置,但是当然我没有annox前缀的定义,所以这不起作用。这篇文章没有说明这是在哪里定义的。
我也在this SO post中查看了多个答案;不幸的是,各种方法中的每一种都省略了一些步骤。例如,我愿意直接调用马歇尔和unmarshall方法,但是我需要知道在哪里可以获得它们所说的“JAXBContext”,以及unmarshall调用是什么样子的,或者在哪里可以查找它。
annox是正确的方法吗?还有其他正确的方法吗?

wnavrhmk

wnavrhmk1#

我刚刚完成了从旧的jaxb 2-maven-plugin转换,它是Maven中只有1个其他项目的依赖项,而maven-jaxb 2-plugin是18个项目的依赖项。似乎有更多的文件。查看JAXB 2 Maven插件Wiki
下面是一个pom.xml示例:

<build>
<plugins>
  <plugin>
    <groupId>org.jvnet.jaxb2.maven2</groupId>
    <artifactId>maven-jaxb2-plugin</artifactId>
    <version>0.13.2</version>
    <executions>
      <execution>
        <id>generate</id>
        <phase>generate-sources</phase>
        <goals>
          <goal>generate</goal>
        </goals>
      </execution>
    </executions>
    <configuration>
      <schemaDirectory>src/main/resources/</schemaDirectory>
      <generateDirectory>${project.build.directory}/generated-sources/jaxb</generateDirectory>
      <schemaIncludes>
        <include>MyXSD.xsd</include>
      </schemaIncludes>
      <schemaExcludes>
        <include>ObeXSD.xsd</include>
      </schemaExcludes>
      <args>
        <arg>-Xannotate</arg>
      </args>
      <plugins>
        <plugin>
            <groupId>org.jvnet.jaxb2_commons</groupId>
            <artifactId>jaxb2-basics-annotate</artifactId>
            <version>1.0.2</version>
        </plugin>
      </plugins>
    </configuration>
  </plugin>
  <plugin>
      <inherited>true</inherited>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-compiler-plugin</artifactId>
      <configuration>
          <source>1.7</source>
          <target>1.7</target>
      </configuration>
  </plugin>

下面是一个xsd示例:

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
    xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
    jaxb:version="2.1" 
    xmlns:annox="http://annox.dev.java.net" 
    jaxb:extensionBindingPrefixes="annox">
    <xsd:complexType name="AbstractProblemClass" abstract="true">
    <xsd:sequence />
    </xsd:complexType>

    <xsd:complexType name="ConcreteClass">
    <xsd:annotation>
        <xsd:appinfo>
            <annox:annotate target="class">@javax.xml.bind.annotation.XmlRootElement</annox:annotate>
        </xsd:appinfo>
    </xsd:annotation>
    <xsd:complexContent>
        <xsd:extension base="AbstractProblemClass">
            <xsd:sequence>
                <xsd:element name="Stuff" type="xsd:String" />
            </xsd:sequence>
        </xsd:extension>
    </xsd:complexContent>
    </xsd:complexType>
</xsd:schema>
aiazj4mn

aiazj4mn2#

对于基于jakarta的注解(Sping Boot 3使用它们,例如),这一个对我有用:

<dependencies>
    <dependency>
        <groupId>jakarta.xml.bind</groupId>
        <artifactId>jakarta.xml.bind-api</artifactId>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>jaxb2-maven-plugin</artifactId>
            <version>${jaxb2-maven-plugin.version}</version>
            <executions>
                <execution>
                    <goals>
                        <goal>xjc</goal>
                    </goals>
                    <configuration>
                        <verbose>true</verbose>
                        <packageName>package.name</packageName>
                        <sourceType>wsdl</sourceType>
                        <xjbSources>
                            <xjbSource>src/main/resources/bindings.xjb</xjbSource>
                        </xjbSources>
                        <sources>
                            <source>src/main/resources/wsdl/some.wsdl</source>
                        </sources>
                        <outputDirectory>target/generated-sources/</outputDirectory>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

bindings.xjb文件:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <jxb:bindings version="3.0" xmlns:jxb="https://jakarta.ee/xml/ns/jaxb"
              xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
              jxb:extensionBindingPrefixes="xjc">
    <jxb:globalBindings>
        <xjc:simple/> <!-- adds @XmlRootElement annotations -->
    </jxb:globalBindings> </jxb:bindings>

相关问题