2

我有两个网络服务。两者都由应用程序实现,因此有一些共同的对象。我有一个 GWT 前端,它同时使用了这两个 web 服务。在这里,当我使用 maven wsimport 生成 webserive 类时。我在两个不同的包中得到了这些相同类的副本。我不想创建这些类的副本。所以创建了这些类的绑定。但每次网络服务发生任何变化。我已经调整了绑定中定义的类,我想避免这种情况。

我的问题是。有没有更好的方法来处理这些常见的类?

4

1 回答 1

0

完全有可能在两个或多个 Web 服务上使用公共 bean。您只需要在两个服务导入的 xsds 中声明您的类型。例如,考虑这个名为 Common.xsd 的 xsd,其中包含一些服务的通用类型

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<xsd:schema version="1.0" xmlns:xsd="http://www.w3.org/2001/XMLSchema"
                         xmlns:tns="http://www.foo.com/ws/common"
                         targetNamespace="http://www.foo.com/ws/common"
                         elementFormDefault="qualified">

    <xsd:element name="Address" type="tns:Address"/>
    <xsd:element name="MyWsFault" type="tns:MyWsFault" />

    <xsd:complexType name="Address">
        <xsd:sequence>
            <xsd:element name="city" type="xsd:string" minOccurs="0"/>
            <xsd:element name="country" type="xsd:string" minOccurs="0"/>
        </xsd:sequence>
    </xsd:complexType>

    <xsd:complexType name="MyWsFault">
        <xsd:sequence>
            <!-- whatever you want to encapsulate in case of an error -->
        </xsd:sequence>
    </xsd:complexType>
</xsd:schema>

现在对于地址类型,将生成一个具有完全限定名称 com.foo.ws.common.Address的类

为了完整起见,请考虑以下服务返回地址(为简单起见,未指定输入)。

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>

<wsdl:definitions xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
                  xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
                  xmlns:xsd="http://www.w3.org/2001/XMLSchema"
                  xmlns:cmn="http://www.foo.com/ws/common"  
                  xmlns:tns="http://www.foo.com/ws"
                  targetNamespace="http://www.foo.com/ws">

    <wsdl:types>
        <xsd:schema>
            <xsd:import namespace="http://www.foo.com/ws/common" schemaLocation="Common.xsd" />
        </xsd:schema>
    </wsdl:types>

    <wsdl:message name="GetAddressRequest">
        <!-- request input here -->
    </wsdl:message>

    <wsdl:message name="GetAddressResponse">
        <wsdl:part name="Address" element="cmn:Address" />
    </wsdl:message>

    <wsdl:message name="MyWsException">
        <wsdl:part name="MyWsFault" element="cmn:MyWsFault" />
    </wsdl:message>

    <wsdl:portType name="GetAddressPortType">
        <wsdl:operation name="GetAddress">
            <wsdl:input message="tns:GetAddressRequest" name="GetAddressRequest"/>
            <wsdl:output message="tns:GetAddressResponse" name="GetAddressResponse"/>
            <wsdl:fault message="tns:MyWsException" name="MyWsException"/>
        </wsdl:operation>
    </wsdl:portType>

    <wsdl:binding name="GetAddressBinding" type="tns:GetAddressPortType">
        <soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"/>
        <wsdl:operation name="GetAddress">
            <soap:operation soapAction="GetAddress"/>
            <wsdl:input name="GetAddressRequest">
                <soap:body use="literal"/>
            </wsdl:input>
            <wsdl:output name="GetAddressResponse">
                <soap:body use="literal"/>
            </wsdl:output>
            <wsdl:fault name="MyWsException">
                <soap:fault name="MyWsException" use="literal"/>
            </wsdl:fault>
        </wsdl:operation>
    </wsdl:binding>

    <wsdl:service name="GetAddressService">
        <wsdl:port binding="tns:GetAddressBinding" name="GetAddressPort">
            <soap:address location="REPLACE_WITH_ACTUAL_URL"/>
        </wsdl:port>
    </wsdl:service>

</wsdl:definitions>

IIRC 在 Spring 上有一个非常好的资源,用于合同优先的 Web 服务,您可能想检查一下。

于 2013-06-10T10:03:19.073 回答