2

I have two webservices. Both are implemented by an application and hence have some common objects. And I have a GWT frontend which uses both of the webservices. And here, when I generate webserive classes using maven wsimport. I get duplicates of these same classes in two different packages. I don't want to create duplicates of these classes. So created bindings of these classes. But everytime anything changes in the webservice. I have adapt the classes defined in bindings, which i would like to avoid.

My question would be. Is there a better way to handle these common classes?

awsome
  • 2,143
  • 2
  • 23
  • 41
  • Use Namespace to package mapping. [This answer][1] has documented a way to do it. [1]: http://stackoverflow.com/questions/6214576/jax-ws-import-and-customizing-package-to-namespace-mapping – Pradeep Pati Jun 10 '13 at 19:54

1 Answers1

0

It is entirely possible to have common beans on two or more web services. You just need to declare your types in xsds that are imported by both services. For example, consider this xsd, named Common.xsd, containing common types for some services

<?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>

Now for the type Address there will be a single class generated with fully qualified name com.foo.ws.common.Address

For completeness sake, consider the following service returning an Address (for simplicity the input is left unspecified).

<?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 there is a pretty good resource on Spring for contract first web services, which you might want to check.

dkateros
  • 1,574
  • 10
  • 14