【问题标题】:CXF wsdl2java listwrapper in wsdl should be unwrappedwsdl 中的 CXF wsdl2java listwrapper 应该被解包
【发布时间】:2015-11-12 08:49:21
【问题描述】:

我目前正在将旧轴 wsclient 更新为 cxf(jaxb 数据绑定)客户端, 现在有区别了,list/array 的处理方式。

让我用一个例子来解释一下:

wsdl

<xsd:complexType name="ArrayOfString">
    <xsd:sequence>
        <xsd:element maxOccurs="unbounded" minOccurs="0" name="string" nillable="true" type="xsd:string"/>
    </xsd:sequence>
</xsd:complexType>

在另一个 complexType 中

<xsd:complexType name="CustomParameter">
    <xsd:sequence>
        <xsd:element minOccurs="0" name="values" nillable="true" type="tns:ArrayOfString"/>
    </xsd:sequence>
</xsd:complexType>

现在,当尝试在 cxf 中访问此属性时,需要另外从列表包装器中获取值

CustomParameter.getValues().getString(); // returns List<String>

Axis 会自动解包,因为您只使用了数组

CustomParameter.getValues() // returns String[]

我的问题是,这可能在 cxf 中做到这一点吗?

我的 wsdl 的一部分:

<?xml version="1.0" encoding="UTF-8"?>
<wsdl:definitions targetNamespace="http://ws611.webservice.adapters.company.de" xmlns:soapenc11="http://schemas.xmlsoap.org/soap/encoding/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
                  xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://ws611.webservice.adapters.company.de" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope"
                  xmlns:wsdlsoap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:soap11="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns2="http://usermanagement.ws611.webservice.company.de"
                  xmlns:ns1="http://ws611.webservice.company.de" xmlns:soapenc12="http://www.w3.org/2003/05/soap-encoding">
    <wsdl:types>
        <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" attributeFormDefault="qualified" elementFormDefault="qualified" targetNamespace="http://ws611.webservice.company.de">
            <xsd:complexType name="AuthenticationToken">
                <xsd:sequence>
                    <xsd:element minOccurs="0" name="password" nillable="true" type="xsd:string"/>
                    <xsd:element minOccurs="0" name="timestamp" nillable="true" type="xsd:string"/>
                    <xsd:element minOccurs="0" name="username" nillable="true" type="xsd:string"/>
                </xsd:sequence>
                <xsd:anyAttribute/>
            </xsd:complexType>
        </xsd:schema>
        <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" attributeFormDefault="qualified" elementFormDefault="qualified" targetNamespace="http://ws611.webservice.adapters.company.de">
            <xsd:element name="getAllUsers">
                <xsd:complexType>
                    <xsd:sequence>
                        <xsd:element maxOccurs="1" minOccurs="1" name="in0" nillable="true" type="ns1:AuthenticationToken"/>
                    </xsd:sequence>
                </xsd:complexType>
            </xsd:element>
            <xsd:element name="getAllUsersResponse">
                <xsd:complexType>
                    <xsd:sequence>
                        <xsd:element maxOccurs="1" minOccurs="1" name="out" nillable="true" type="ns2:ArrayOfUser"/>
                    </xsd:sequence>
                </xsd:complexType>
            </xsd:element>
        </xsd:schema>
        <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" attributeFormDefault="qualified" elementFormDefault="qualified" targetNamespace="http://usermanagement.ws611.webservice.company.de">
            <xsd:complexType name="ArrayOfUser">
                <xsd:sequence>
                    <xsd:element maxOccurs="unbounded" minOccurs="0" name="User" nillable="true" type="ns2:User"/>
                </xsd:sequence>
            </xsd:complexType>
            <xsd:complexType name="User">
                <xsd:sequence>
                    <xsd:element minOccurs="0" name="name" nillable="true" type="xsd:string"/>
                    <xsd:element minOccurs="0" name="groups" nillable="true" type="ns2:ArrayOfGroup"/>
                </xsd:sequence>
                <xsd:anyAttribute/>
            </xsd:complexType>
            <xsd:complexType name="ArrayOfGroup">
                <xsd:sequence>
                    <xsd:element maxOccurs="unbounded" minOccurs="0" name="Group" nillable="true" type="ns2:Group"/>
                </xsd:sequence>
            </xsd:complexType>
            <xsd:complexType name="Group">
                <xsd:sequence>
                    <xsd:element minOccurs="0" name="name" nillable="true" type="xsd:string"/>
                </xsd:sequence>
                <xsd:anyAttribute/>
            </xsd:complexType>
        </xsd:schema>
    </wsdl:types>
    <wsdl:message name="getAllUsersRequest">
        <wsdl:part name="parameters" element="tns:getAllUsers">
        </wsdl:part>
    </wsdl:message>
    <wsdl:message name="getAllUsersResponse">
        <wsdl:part name="parameters" element="tns:getAllUsersResponse">
        </wsdl:part>
    </wsdl:message>
    <wsdl:portType name="UserManagementPortType">
        <wsdl:operation name="getAllUsers">
            <wsdl:input name="getAllUsersRequest" message="tns:getAllUsersRequest">
            </wsdl:input>
            <wsdl:output name="getAllUsersResponse" message="tns:getAllUsersResponse">
            </wsdl:output>
        </wsdl:operation>
    </wsdl:portType>
    <wsdl:binding name="UserManagementHttpBinding" type="tns:UserManagementPortType">
        <wsdlsoap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
        <wsdl:operation name="getAllUsers">
            <wsdlsoap:operation soapAction=""/>
            <wsdl:input name="getAllUsersRequest">
                <wsdlsoap:body use="literal"/>
            </wsdl:input>
            <wsdl:output name="getAllUsersResponse">
                <wsdlsoap:body use="literal"/>
            </wsdl:output>
        </wsdl:operation>
    </wsdl:binding>
    <wsdl:service name="UserManagement">
        <wsdl:port name="UserManagementHttpPort" binding="tns:UserManagementHttpBinding">
            <wsdlsoap:address location="http://localhost:8080/test/webservice/ws611/UserManagement"/>
        </wsdl:port>
    </wsdl:service>
</wsdl:definitions>

【问题讨论】:

    标签: java jaxb cxf wsdl2java cxf-codegen-plugin


    【解决方案1】:

    可以通过cxf-codegen-plugin 生成自动隐藏 XML 元素包装类的模型类。

    你需要用到三样东西:

    1. 一个custom JAXB binding file that suppresses usage of JAXBElement
    2. 对于任何 PortType,custom JAXWS binding file 确保直接使用您的请求和响应类
    3. jaxb-xew-plugin 用于生成 @XmlElementWrapper 注释,这是用于将列表指定为已包装的 JAXB 注释

    自定义 JAXB 绑定文件应如下所示:

    <jaxb:bindings version="2.1" xmlns:jaxb="http://java.sun.com/xml/ns/jaxb">
        <jaxb:globalBindings generateElementProperty="false"/>
    </jaxb:bindings>
    

    自定义 JAXWS 绑定文件应如下所示:

    <jaxws:bindings xmlns:jaxws="http://java.sun.com/xml/ns/jaxws">
        <jaxws:enableWrapperStyle>false</jaxws:enableWrapperStyle>
    </jaxws:bindings>
    

    这是使用 JAXB 和 JAXWS 绑定文件以及 jaxb-xew-plugincxf-codegen-plugin 的示例工作用法:

    <plugin>
        <groupId>org.apache.cxf</groupId>
        <artifactId>cxf-codegen-plugin</artifactId>
        <version>3.1.4</version>
        <executions>
            <execution>
                <id>generate-client</id>
                <phase>generate-sources</phase>
                <configuration>
                    <wsdlOptions>
                        <wsdlOption>
                            <wsdl>${basedir}/src/main/resources/sample.wsdl</wsdl>
                            <bindingFiles>
                                <bindingFile>${basedir}/src/main/resources/jaxbBinding.xml</bindingFile>
                                <bindingFile>${basedir}/src/main/resources/jaxwsBinding.xml</bindingFile>
                            </bindingFiles>
                            <extraargs>
                                <extraarg>-xjc-Xxew</extraarg>
                                <extraarg>-xjc-Xxew:summary
                                    ${project.build.outputDirectory}/../xew-summary.txt
                                </extraarg>
                                <extraarg>-xjc-Xxew:instantiate lazy</extraarg>
                            </extraargs>
                        </wsdlOption>
                    </wsdlOptions>
                </configuration>
                <goals>
                    <goal>wsdl2java</goal>
                </goals>
            </execution>
        </executions>
        <dependencies>
            <dependency>
                <groupId>com.github.jaxb-xew-plugin</groupId>
                <artifactId>jaxb-xew-plugin</artifactId>
                <version>1.6</version>
            </dependency>
            <dependency>
                <groupId>com.sun.xml.bind</groupId>
                <artifactId>jaxb-xjc</artifactId>
                <version>2.2.11</version>
            </dependency>
        </dependencies>
    </plugin>
    

    此配置将在问题中提供的架构上生成以下内容:

    CustomParameter.getValues() // returns List<String>
    

    请注意,这不会返回 String[]。您可以通过将collectionType="indexed" 属性添加到您的globalBindings 来让cxf-codegen-plugin 使用String[] 而不是List&lt;String&gt;,但目前jaxb-xew-plugin 仅支持Collection 类型而不支持数组。

    【讨论】:

    • 我试过这个插件,但是生成的代码有编译错误,找不到ArrayOfUser,我用wsdl编辑了我的问题重现,插件删除了ArrayOfUser但在我的* PortType,还在使用。
    • @JannisM 我用有关如何指定 JAXWS 绑定文件以确保正确生成 PortTypes 的说明更新了答案。
    • 但是当enableWrapperStylefalse时,又是GetAllUsersGetAllUsersResponse的一个换行,看起来更糟糕,然后是另一个,所以没有其他拥有enableWrapperStyle=true 并且没有列表包装类的方式?
    • @JannisM GetAllUsers 无论如何都存在。将enableWrapperStyle 设置为false 会更改UserManagementPortType 以返回带有单个List&lt;User&gt; outGetAllUsersResponse,而不是带有单个List&lt;User&gt; userArrayOfUser,因此不会更糟。你想让UserManagementPortType.getAllUsers 直接返回List&lt;User&gt; 吗?这可能是不可能的。
    • 没关系,如果UserManagementPortType#getAllUsers 将返回ArrayOfUsersArrayOf* 作为类属性应该转换为List,但这似乎是不可配置的还是?
    猜你喜欢
    • 1970-01-01
    • 2011-07-02
    • 2021-04-26
    • 1970-01-01
    • 2018-01-19
    • 2012-10-05
    • 1970-01-01
    • 2015-06-14
    • 1970-01-01
    相关资源
    最近更新 更多