【问题标题】:Want @XmlRootElement in classes generated by jaxb2-maven-plugin在 jaxb2-maven-plugin 生成的类中需要 @XmlRootElement
【发布时间】:2019-06-21 00:15:46
【问题描述】:

我有一组从 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>

这里是 bindings.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 中读到了关于使用annotate 标签的帖子,所以我插入了

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

在 bindings.xjb 文件中的指定位置,但我当然没有 annox 前缀的定义,所以这不起作用。该帖子没有说明定义的位置。

我在this SO 帖子中也经历过多个答案;不幸的是,各种方法都遗漏了一些步骤。例如,我愿意直接调用 marshall 和 unmarshall 方法,但我需要知道从哪里获取他们所说的“JAXBContext”,以及 unmarshall 调用是什么样的,或者在哪里查找它。

annox 是正确的方法吗?还有其他正确的方法吗?

【问题讨论】:

  • 你能发布 w1/w2.wsdl(也许是简化形式)吗?
  • @ScottKurz 会很高兴,但必须等到星期一(美国东部时间);刚刚尝试进入我的工作计算机,但登录顺序有问题...
  • jaxb2-maven-plugin 最简单的方法在这里stackoverflow.com/a/10613686

标签: java maven jaxb wsdl


【解决方案1】:

我刚刚完成从旧的 jaxb2-maven-plugin 转换为 maven-jaxb2-plugin,它是 Maven 中仅 1 个其他项目的依赖项,它是 18 的依赖项。似乎有更多的文档.查看JAXB2 Maven Plugin 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>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-03-31
    • 1970-01-01
    • 1970-01-01
    • 2015-09-20
    • 1970-01-01
    • 1970-01-01
    • 2013-12-18
    • 1970-01-01
    相关资源
    最近更新 更多