【问题标题】:Missing classes in @XmlSeeAlso annotation of generated Java class生成的 Java 类的 @XmlSeeAlso 注释中缺少类
【发布时间】:2019-07-10 11:22:58
【问题描述】:

我正在使用 Maven 的 cxf-codegen-plugin 从 wsdl/xsd 文件生成 Java 类(然后基于它们打包一个 jar)。我之所以选择这个插件而不是其他选项,是因为它允许我“动态地”引用文件夹中的所有 wsdl 和 xsd 文件,而不是“静态地”将它们一一写在 <wsdl/> 标签中。

FurtherExtensionOfBaseObj Java 类(在internet.xsd 文件中定义)由MyBarRequest(来自bar.wsdl)和MyZedRequest(来自zed.wsdl)实现,但仅在其@XmlSeeAlso 注释中实现@ 987654331@被引用。

就好像插件只考虑了他在第一个处理的 wsdl 中找到的内容(按字母顺序排列)而不考虑其他内容。 我怎样才能获得插件(或任何其他插件,尽管我喜欢这个)@XmlSeeAlso 注释中引用所有扩展类

您可以在https://gitlab.com/t.meledina/xmlseealso-issue-poc 上提取一个完全重现此问题的工作项目

编辑:问题现已解决,存储库现在是下面 Tomas 解决方案的工作示例。

或者,可以使用以下文件列表来重现问题,common.xsd 应该无关紧要,但它是编译所必需的。

common.xsd 文件

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema targetNamespace="http://www.somehost.com/common/beans" xmlns:beans="http://www.somehost.com/common/beans" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <xsd:complexType name="SomeCommonObj">
        <xsd:sequence>
            <xsd:element name="woop" type="xsd:int"/>
        </xsd:sequence>
    </xsd:complexType>
    <xsd:complexType name="ExtensionOfSomeCommonObj">
        <xsd:complexContent>
            <xsd:extension base="beans:SomeCommonObj">
                <xsd:sequence>
                    <xsd:element name="moreWoop" type="xsd:string"/>
                </xsd:sequence>
            </xsd:extension>
        </xsd:complexContent>
    </xsd:complexType>
    <xsd:complexType name="BaseRequest">
        <xsd:sequence>
            <xsd:element name="floop" type="xsd:int"/>
        </xsd:sequence>
    </xsd:complexType>
    <xsd:complexType name="BaseResponse">
        <xsd:sequence>
            <xsd:element name="scoop" type="xsd:int"/>
        </xsd:sequence>
    </xsd:complexType>
    <xsd:complexType name="AnotherCommonObj">
        <xsd:annotation>
            <xsd:documentation>whatever</xsd:documentation>
        </xsd:annotation>
        <xsd:sequence/>
    </xsd:complexType>
    <xsd:complexType name="ExtensionOfAnotherCommonObj">
        <xsd:annotation>
            <xsd:documentation>son of whatever</xsd:documentation>
        </xsd:annotation>
        <xsd:complexContent>
            <xsd:extension base="beans:AnotherCommonObj">
                <xsd:sequence>
                    <xsd:element minOccurs="1" name="goop" type="xsd:int"/>
                </xsd:sequence>
            </xsd:extension>
        </xsd:complexContent>
    </xsd:complexType>
    <xsd:complexType name="BarObj">
        <xsd:sequence>
            <xsd:element name="noop" type="xsd:string"/>
        </xsd:sequence>
    </xsd:complexType>
</xsd:schema>

internet.xsd 文件

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema targetNamespace="http://www.somehost.com/internet/beans" xmlns:tns="http://www.somehost.com/internet/beans" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <xsd:import namespace="http://www.somehost.com/common/beans" schemaLocation="common.xsd"/>
    <xsd:complexType name="BaseObj">
        <xsd:sequence>
            <xsd:element name="zeep" type="xsd:int"/>
        </xsd:sequence>
    </xsd:complexType>
    <xsd:complexType name="ExtensionOfBaseObj">
        <xsd:complexContent>
            <xsd:extension base="tns:BaseObj">
                <xsd:sequence>
                    <xsd:element name="moreZeep" type="xsd:int"/>
                </xsd:sequence>
            </xsd:extension>
        </xsd:complexContent>
    </xsd:complexType>
    <xsd:complexType name="SomeOtherObj">
        <xsd:sequence>
            <xsd:element name="neep" type="xsd:string"/>
        </xsd:sequence>
    </xsd:complexType>
    <xsd:complexType name="YetSomeOtherObj">
        <xsd:sequence>
            <xsd:element name="geep" type="xsd:dateTime"/>
        </xsd:sequence>
    </xsd:complexType>
    <xsd:complexType name="FurtherExtensionOfBaseObj">
        <xsd:complexContent>
            <xsd:extension base="tns:ExtensionOfBaseObj">
                <xsd:sequence>
                    <xsd:element name="evenMoreZeep" type="xsd:string"/>
                </xsd:sequence>
            </xsd:extension>
        </xsd:complexContent>
    </xsd:complexType>
</xsd:schema>

bar.wsdl 文件

<?xml version="1.0" encoding="UTF-8"?>
<wsdl:definitions name="bar" targetNamespace="http://www.somehost.com/internet/bar/" xmlns:beans="http://www.somehost.com/internet/bar/beans/" xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/" xmlns:tns="http://www.somehost.com/internet/bar/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <wsdl:types>
        <xsd:schema jaxb:version="2.0" targetNamespace="http://www.somehost.com/internet/bar/beans/" xmlns:common="http://www.somehost.com/common/beans" xmlns:internet="http://www.somehost.com/internet/beans" xmlns:jaxb="http://java.sun.com/xml/ns/jaxb">
            <xsd:import namespace="http://www.somehost.com/common/beans" schemaLocation="common.xsd"/>
            <xsd:import namespace="http://www.somehost.com/internet/beans" schemaLocation="internet.xsd"/>
            <xsd:annotation>
                <xsd:appinfo>
                    <jaxb:globalBindings>
                        <jaxb:javaType name="java.util.Calendar" parseMethod="javax.xml.bind.DatatypeConverter.parseDateTime" printMethod="javax.xml.bind.DatatypeConverter.printDateTime" xmlType="xsd:dateTime"/>
                    </jaxb:globalBindings>
                </xsd:appinfo>
            </xsd:annotation>
            <xsd:complexType name="myBarRequest">
                <xsd:complexContent>
                    <xsd:extension base="internet:FurtherExtensionOfBaseObj">
                        <xsd:sequence>
                            <xsd:element minOccurs="0" name="barObjAttuale" type="common:BarObj"/>
                            <xsd:element name="barObjNuovo" type="common:BarObj"/>
                        </xsd:sequence>
                    </xsd:extension>
                </xsd:complexContent>
            </xsd:complexType>
            <xsd:element name="myBarRequestElement" type="beans:myBarRequest"/>
            <xsd:complexType name="myBarResponse">
                <xsd:complexContent>
                    <xsd:extension base="common:BaseResponse">
                        <xsd:sequence minOccurs="0">
                            <xsd:element name="barObj" type="common:BarObj"/>
                        </xsd:sequence>
                    </xsd:extension>
                </xsd:complexContent>
            </xsd:complexType>
            <xsd:element name="myBarResponseElement" type="beans:myBarResponse"/>
        </xsd:schema>
    </wsdl:types>
    <wsdl:message name="myBarRequest">
        <wsdl:part element="beans:myBarRequestElement" name="parameters"/>
    </wsdl:message>
    <wsdl:message name="myBarResponse">
        <wsdl:part element="beans:myBarResponseElement" name="parameters"/>
    </wsdl:message>
    <wsdl:portType name="barPortType">
        <wsdl:operation name="myBar">
            <wsdl:input message="tns:myBarRequest"/>
            <wsdl:output message="tns:myBarResponse"/>
        </wsdl:operation>
    </wsdl:portType>
    <wsdl:binding name="barBinding" type="tns:barPortType">
        <soap12:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
        <wsdl:operation name="myBar">
            <wsdl:input>
                <soap12:body use="literal"/>
            </wsdl:input>
            <wsdl:output>
                <soap12:body use="literal"/>
            </wsdl:output>
        </wsdl:operation>
    </wsdl:binding>
    <wsdl:service name="barService">
        <wsdl:port binding="tns:barBinding" name="barPort">
            <soap12:address location="http://localhost:9080/internet/barService"/>
        </wsdl:port>
    </wsdl:service>
</wsdl:definitions>

zed.wsdl 文件

<?xml version="1.0" encoding="UTF-8"?>
<wsdl:definitions name="zedService" targetNamespace="http://www.somehost.com/internet/zed/" xmlns:beans="http://www.somehost.com/internet/zed/beans/" xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/" xmlns:tns="http://www.somehost.com/internet/zed/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <wsdl:types>
        <xsd:schema jaxb:version="2.0" targetNamespace="http://www.somehost.com/internet/zed/beans/" xmlns:common="http://www.somehost.com/common/beans" xmlns:internet="http://www.somehost.com/internet/beans" xmlns:jaxb="http://java.sun.com/xml/ns/jaxb">
            <xsd:import namespace="http://www.somehost.com/common/beans" schemaLocation="common.xsd"/>
            <xsd:import namespace="http://www.somehost.com/internet/beans" schemaLocation="internet.xsd"/>
            <xsd:annotation>
                <xsd:appinfo>
                    <jaxb:globalBindings>
                        <jaxb:javaType name="java.util.Calendar" parseMethod="javax.xml.bind.DatatypeConverter.parseDateTime" printMethod="javax.xml.bind.DatatypeConverter.printDateTime" xmlType="xsd:dateTime"/>
                    </jaxb:globalBindings>
                </xsd:appinfo>
            </xsd:annotation>
            <xsd:complexType name="myZedRequest">
                <xsd:complexContent>
                    <xsd:extension base="internet:FurtherExtensionOfBaseObj">
                        <xsd:sequence>
                            <xsd:element minOccurs="1" name="blaap" type="xsd:long"/>
                        </xsd:sequence>
                    </xsd:extension>
                </xsd:complexContent>
            </xsd:complexType>
            <xsd:element name="myZedRequestElement" type="beans:myZedRequest"/>
            <xsd:complexType name="myZedResponse">
                <xsd:complexContent>
                    <xsd:extension base="common:BaseResponse">
                        <xsd:sequence minOccurs="0"/>
                    </xsd:extension>
                </xsd:complexContent>
            </xsd:complexType>
            <xsd:element name="myZedResponseElement" type="beans:myZedResponse"/>
        </xsd:schema>
    </wsdl:types>
    <wsdl:message name="myZedRequest">
        <wsdl:part element="beans:myZedRequestElement" name="parameters"/>
    </wsdl:message>
    <wsdl:message name="myZedResponse">
        <wsdl:part element="beans:myZedResponseElement" name="parameters"/>
    </wsdl:message>
    <wsdl:portType name="zedPortType">
        <wsdl:operation name="myZed">
            <wsdl:input message="tns:myZedRequest"/>
            <wsdl:output message="tns:myZedResponse"/>
        </wsdl:operation>
    </wsdl:portType>
    <wsdl:binding name="zedBinding" type="tns:zedPortType">
        <soap12:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
        <wsdl:operation name="myZed">
            <wsdl:input>
                <soap12:body use="literal"/>
            </wsdl:input>
            <wsdl:output>
                <soap12:body use="literal"/>
            </wsdl:output>
        </wsdl:operation>
    </wsdl:binding>
    <wsdl:service name="zedService">
        <wsdl:port binding="tns:zedBinding" name="zedPort">
            <soap12:address location="http://localhost:9080/internet/zedService"/>
        </wsdl:port>
    </wsdl:service>
</wsdl:definitions>

pom.xml 文件

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.mytest.mobile</groupId>
    <artifactId>mytest-jax-ws</artifactId>
    <version>20190707-1950</version>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.cxf</groupId>
                <artifactId>cxf-codegen-plugin</artifactId>
                <version>3.1.17</version>
                <executions>
                    <execution>
                        <id>generate-sources</id>
                        <phase>generate-sources</phase>
                        <configuration>
                            <wsdlRoot>src/main/resources/wsdl</wsdlRoot>
                            <includes>
                                <include>*.wsdl</include>
                            </includes>
                        </configuration>
                        <goals>
                            <goal>wsdl2java</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <artifactId>maven-clean-plugin</artifactId>
                <version>2.4.1</version>
                <executions>
                    <execution>
                        <id>auto-clean</id>
                        <phase>initialize</phase>
                        <goals>
                            <goal>clean</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
    <dependencies>
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-codegen-plugin</artifactId>
            <version>3.2.7</version>
        </dependency>
    </dependencies>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.7</maven.compiler.source>
        <maven.compiler.target>1.7</maven.compiler.target>
    </properties>
</project>

提前感谢您的帮助。

【问题讨论】:

    标签: java maven soap xsd wsdl


    【解决方案1】:

    我已经使用适当的注释通过 XSD 生成显式覆盖了从 WSDL 生成生成的类。请将整个文本添加到 pom.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <groupId>com.test</groupId>
        <artifactId>my-jax-ws</artifactId>
        <version>20190707-1950</version>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.cxf</groupId>
                    <artifactId>cxf-codegen-plugin</artifactId>
                    <version>3.1.17</version>
                    <executions>
                        <execution>
                            <id>generate-sources</id>
                            <phase>generate-sources</phase>
                            <configuration>
                                <wsdlRoot>src/main/resources/wsdl</wsdlRoot>
                                <includes>
                                    <include>*.wsdl</include>
                                </includes>
                             </configuration>
                            <goals>
                                <goal>wsdl2java</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
                <plugin>
                    <groupId>org.apache.cxf</groupId>
                    <artifactId>cxf-xjc-plugin</artifactId>
                    <version>3.3.1</version>
                    <executions>
                        <execution>
                            <id>generate-sources</id>
                            <phase>generate-sources</phase>
                            <configuration>
                                <sourceRoot>${basedir}/target/generated-sources/cxf</sourceRoot>
                                <xsdOptions>
                                    <xsdOption>
                                        <xsd>src/main/resources/wsdl/internet.xsd</xsd>
                                        <bindingFiles>
                                            <bindingFile>src/main/resources/bindings.xjb</bindingFile>
                                        </bindingFiles>
                                        <extension>true</extension>
                                        <extensionArgs>
                                            <extensionArg>-Xannotate</extensionArg>
                                        </extensionArgs>
                                    </xsdOption>
                                </xsdOptions>
                            </configuration>
                            <goals>
                                <goal>xsdtojava</goal>
                            </goals>
                        </execution>
                    </executions>
                    <dependencies>
                        <dependency>
                            <groupId>org.jvnet.jaxb2_commons</groupId>
                            <artifactId>jaxb2-basics</artifactId>
                            <version>1.11.1</version>
                        </dependency>
                        <dependency>
                            <groupId>org.jvnet.jaxb2_commons</groupId>
                            <artifactId>jaxb2-basics-annotate</artifactId>
                            <version>1.0.2</version>
                        </dependency>
                    </dependencies>
                </plugin>
                <plugin>
                    <artifactId>maven-clean-plugin</artifactId>
                    <version>2.4.1</version>
                    <executions>
                        <execution>
                            <id>auto-clean</id>
                            <phase>initialize</phase>
                            <goals>
                                <goal>clean</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </build>
        <dependencies>
            <dependency>
                <groupId>org.apache.cxf</groupId>
                <artifactId>cxf-codegen-plugin</artifactId>
                <version>3.2.7</version>
            </dependency>
        </dependencies>
        <properties>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <maven.compiler.source>1.7</maven.compiler.source>
            <maven.compiler.target>1.7</maven.compiler.target>
        </properties>
    </project>
    

    并在文件夹中创建 bindings.xjb 文件:xmlseealso-issue-poc\src\main\resources 使用以下代码:

    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <jaxb:bindings xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" xmlns:xsd="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"
                   version="2.1">
    
            <jaxb:bindings schemaLocation="wsdl\internet.xsd" node="/xsd:schema">
            <jaxb:bindings node="xsd:complexType[@name='FurtherExtensionOfBaseObj']">
                    <annox:annotate target="class">
                        @javax.xml.bind.annotation.XmlSeeAlso({com.somehost.internet.zed.beans.MyZedRequest.class, com.somehost.internet.bar.beans.MyBarRequest.class})
                    </annox:annotate>
            </jaxb:bindings>
        </jaxb:bindings>
    
    
    </jaxb:bindings>
    

    希望对你有帮助。

    【讨论】:

    • 这是 100% 我一直在寻找的东西,它就像一个魅力,除了我在给出 schemaLocation 时必须替换您在 bindings.xjb 中使用的反斜杠(“wsdl\internet.xsd ") 带有正斜杠“wsdl/internet.xsd”。
    【解决方案2】:

    为了能够使用cxf-codegen-plugin 的WSDL 根选项,您还必须提供JAXB 绑定文件(参见documentation of the plugin)。也可以生成绑定XML,参考this SO answer

    【讨论】:

    • 感谢您的回答。您能否澄清一下“为了能够使用 cxf-codegen-plugin 的 WSDL-root 选项,您也必须提供 JAXB 绑定文件”是什么意思?该选项已经有效(即它确实构建了 .java 和 .class 文件)。我也找不到从您链接的答案中“生成”绑定文件(我猜您的意思是通过插件)的方法,它看起来更像是一些关于如何写下来的提示。或许您可以草拟一个绑定文件来补充我在此处引用的示例文件?
    【解决方案3】:

    我最近遇到一个问题,我想在从 WSDL 文件生成的类中添加注释 @XmlSeeAlso,请在下面找到代码示例。这可能会对您有所帮助:

    pom.xml

    <plugin>
        <groupId>org.apache.cxf</groupId>
        <artifactId>cxf-codegen-plugin</artifactId>
        <version>3.0.9</version>
        <executions>
            <execution>
                <id>generate-sources-referencedata</id>
                <phase>generate-sources</phase>
                <configuration>
                    <sourceRoot>${project.build.directory}/generated-sources/cxf</sourceRoot>
                    <defaultOptions>
                        <frontEnd>jaxws21</frontEnd>
                        <faultSerialVersionUID>1</faultSerialVersionUID>
                        <noAddressBinding>true</noAddressBinding>
                    </defaultOptions>
                    <wsdlOptions>
                        <wsdlOption>
                            <wsdlLocation>classpath:ReferenceDataService.wsdl</wsdlLocation>
                            <wsdl>src/main/resources/ReferenceDataService.wsdl</wsdl>
                            <bindingFiles>
                                <bindingFile>src/main/resources/bindings.xjb</bindingFile> 
                            </bindingFiles>
                            <extraargs>
                                <extraarg>-xjc-Xannotate</extraarg>
                            </extraargs>
                        </wsdlOption>
                    </wsdlOptions>
                </configuration>
                <goals>
                    <goal>wsdl2java</goal>
                </goals>
            </execution>
        </executions>
        <dependencies>
            <dependency>
                <groupId>org.jvnet.jaxb2_commons</groupId>
                <artifactId>jaxb2-basics</artifactId>
                <version>1.11.1</version>
            </dependency>
            <dependency>
                <groupId>org.jvnet.jaxb2_commons</groupId>
                <artifactId>jaxb2-basics-annotate</artifactId>
                <version>1.0.2</version>
            </dependency>
        </dependencies>
    </plugin>
    

    bindings.xjb [外部配置文件]

    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <jaxws:bindings
            xmlns:jaxws="http://java.sun.com/xml/ns/jaxws"
            xmlns:xs="http://www.w3.org/2001/XMLSchema"
            xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
            xmlns:annox="http://annox.dev.java.net"
            xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
            jaxb:extensionBindingPrefixes="annox">
    
        <jaxws:bindings schemaLocation="ReferenceDataService.wsdl">
            <jaxws:bindings node="wsdl:definitions/wsdl:types/xs:schema">
                <jaxb:bindings node="xs:complexType[@name='ReferenceDataRequestCType']">
                    <annox:annotate target="class">
                        @javax.xml.bind.annotation.XmlSeeAlso({com.project.BindingClass.class})
                    </annox:annotate>
                </jaxb:bindings>
            </jaxws:bindings>
        </jaxws:bindings>
    </jaxws:bindings>
    

    【讨论】:

    • 非常感谢您提供的样品。不幸的是,我似乎无法使其工作(在更改名称 na dNS 以明显匹配我的项目之后),即执行没有错误,但注释中缺少的类仍然丢失。你能不能让它在我提供的示例项目中工作?无论哪种方式都非常感谢。
    • 我无法预先推送到您的项目,所以我会在这里写另一个答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-05-06
    • 2019-09-07
    • 1970-01-01
    • 2019-04-18
    • 2016-05-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多