Saturday, May 3, 2008

Webservices – understanding wsdl

Here, we have a sample wsdl file, we are going to figure out how the webservice implementation class looks like. (This may help you later to understand how to invoke webservice operations – in a client – given a wsdl file).

Now given a simple wsdl file:

<?xml version="1.0" encoding="UTF-8"?>
<wsdl:definitions targetNamespace="http://test" xmlns:apachesoap="http://xml.apache.org/xml-soap" xmlns:impl="http://test" xmlns:intf="http://test" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:wsdlsoap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<wsdl:types>
<schema elementFormDefault="qualified" targetNamespace="http://test" xmlns="http://www.w3.org/2001/XMLSchema">
<element name="sayHi">
<complexType>
<sequence>
<element name="name" type="xsd:string"/>
</sequence>
</complexType>
</element>
<element name="sayHiResponse">
<complexType>
<sequence>
<element name="sayHiReturn" type="xsd:string"/>
</sequence>
</complexType>
</element>
<element name="sqPower">
<complexType>
<sequence>
<element name="num" type="xsd:int"/>
</sequence>
</complexType>
</element>
<element name="sqPowerResponse">
<complexType>
<sequence>
<element name="sqPowerReturn" type="xsd:int"/>
</sequence>
</complexType>
</element>
</schema>
</wsdl:types>

<wsdl:message name="sqPowerResponse">

<wsdl:part element="impl:sqPowerResponse" name="parameters"/>

</wsdl:message>

<wsdl:message name="sayHiRequest">

<wsdl:part element="impl:sayHi" name="parameters"/>

</wsdl:message>

<wsdl:message name="sqPowerRequest">

<wsdl:part element="impl:sqPower" name="parameters"/>

</wsdl:message>

<wsdl:message name="sayHiResponse">

<wsdl:part element="impl:sayHiResponse" name="parameters"/>

</wsdl:message>

<wsdl:portType name="MainService">

<wsdl:operation name="sayHi">

<wsdl:input message="impl:sayHiRequest" name="sayHiRequest"/>

<wsdl:output message="impl:sayHiResponse" name="sayHiResponse"/>

</wsdl:operation>

<wsdl:operation name="sqPower">

<wsdl:input message="impl:sqPowerRequest" name="sqPowerRequest"/>

<wsdl:output message="impl:sqPowerResponse" name="sqPowerResponse"/>

</wsdl:operation>

</wsdl:portType>

<wsdl:binding name="MainServiceSoapBinding" type="impl:MainService">

<wsdlsoap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>

<wsdl:operation name="sayHi">

<wsdlsoap:operation soapAction=""/>

<wsdl:input name="sayHiRequest">

<wsdlsoap:body use="literal"/>

</wsdl:input>

<wsdl:output name="sayHiResponse">

<wsdlsoap:body use="literal"/>

</wsdl:output>

</wsdl:operation>

<wsdl:operation name="sqPower">

<wsdlsoap:operation soapAction=""/>

<wsdl:input name="sqPowerRequest">

<wsdlsoap:body use="literal"/>

</wsdl:input>

<wsdl:output name="sqPowerResponse">

<wsdlsoap:body use="literal"/>

</wsdl:output>

</wsdl:operation>

</wsdl:binding>

<wsdl:service name="MainServiceService">

<wsdl:port binding="impl:MainServiceSoapBinding" name="MainService">

<wsdlsoap:address location="http://localhost:8080/service/services/MainService"/>

</wsdl:port>

</wsdl:service>

</wsdl:definitions>


By analyzing the given wsdl we have:

- One class implementing the webservice; one 'portType' element, with name 'MainService'
- including two methods; two <operation> elements inside the <portType>, named 'sayHi' and 'sqPower'

- 'sayHi' method has the following parameters:
- one input parameter - message (<input message="impl:sqPowerRequest>) of type String and name 'name' (<element name="sayHi"> ... <element name="name" type="xsd:string"/> ... </element>)
- one return type – output message (<output message="impl:sayHiResponse">) of type String (<element name="sayHiResponse"> ... <element name="sayHiReturn" type="xsd:string"/> ... </element> )

- 'sqPower' method has the following parameters:
- one input parameter of type int and name 'num' (<element name="num" type="xsd:int"/>)
- one return type – output message (<output message="impl:sqPowerResponse">) of type int (<element name="sqPowerReturn" type="xsd:int"/>)

- Target namespace including web service definitions is 'http://test'; defined in the root element - namespaces definitions (<wsdl:definitions xmlns:impl="http://test")

- Communication details: Messaging protocol is SOAP using transport protocol 'http', both defined inside '<wsdl:binding>' element; (<wsdlsoap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>)

- URL to access webservice: http://localhost:8080/service/services/MainService
(<wsdlsoap:address location="http://localhost:8080/service/services/MainService"/>)


Now, we conclude the webservice implementation class will look like this:

public class MainService{

public MainService(){}

public String sayHi(String name)
{
// some code
}

public int sqPower(int num)
{
// some code
}
}

For understanding how complex parameters (like Vector) are represented in wsdl, please refer to: http://www.ibm.com/developerworks/library/ws-intwsdl/

No comments: