Master java skills

WSDL

WSDL stands for Web Services Description Language. Below are some important points about wsdl.

  1. WSDL is used to describe web services
  2. WSDL is a contract pubilshed by web service producer
  3. WSDL is a xml based document
  4. Using WSDL file, client can generate required code at its end and consume the web service
  5. If WSDL changes, the contract between publisher and consumer changes. And the client code needs to be generated according to the new WSDL file
  6. WSDL is a W3C recommendation from 26. June 2007

Sample wsdl file

Below is a sample wsdl file

<?xml version="1.0" encoding="UTF-8"?>
<wsdl:definitions targetNamespace="http://sks.com" xmlns:apachesoap="http://xml.apache.org/xml-soap" xmlns:impl="http://sks.com" xmlns:intf="http://sks.com" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:wsdlsoap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<!--WSDL created by Apache Axis version: 1.4
Built on Apr 22, 2006 (06:55:48 PDT)-->
 <wsdl:types>
  <schema elementFormDefault="qualified" targetNamespace="http://sks.com" xmlns="http://www.w3.org/2001/XMLSchema">
   <element name="sum">
    <complexType>
     <sequence>
      <element name="op" type="impl:Operand"/>
     </sequence>
    </complexType>
   </element>
   <complexType name="Operand">
    <sequence>
     <element name="num1" type="xsd:int"/>
     <element name="num2" type="xsd:int"/>
    </sequence>
   </complexType>
   <element name="sumResponse">
    <complexType>
     <sequence>
      <element name="sumReturn" type="xsd:int"/>
     </sequence>
    </complexType>
   </element>
   <element name="multiplication">
    <complexType>
     <sequence>
      <element name="op" type="impl:Operand"/>
     </sequence>
    </complexType>
   </element>
   <element name="multiplicationResponse">
    <complexType>
     <sequence>
      <element name="multiplicationReturn" type="xsd:int"/>
     </sequence>
    </complexType>
   </element>
  </schema>
 </wsdl:types>

   <wsdl:message name="multiplicationRequest">

      <wsdl:part element="impl:multiplication" name="parameters">

      </wsdl:part>

   </wsdl:message>

   <wsdl:message name="sumRequest">

      <wsdl:part element="impl:sum" name="parameters">

      </wsdl:part>

   </wsdl:message>

   <wsdl:message name="sumResponse">

      <wsdl:part element="impl:sumResponse" name="parameters">

      </wsdl:part>

   </wsdl:message>

   <wsdl:message name="multiplicationResponse">

      <wsdl:part element="impl:multiplicationResponse" name="parameters">

      </wsdl:part>

   </wsdl:message>

   <wsdl:portType name="CalculationServiceImpl">

      <wsdl:operation name="sum">

         <wsdl:input message="impl:sumRequest" name="sumRequest">

       </wsdl:input>

         <wsdl:output message="impl:sumResponse" name="sumResponse">

       </wsdl:output>

      </wsdl:operation>

      <wsdl:operation name="multiplication">

         <wsdl:input message="impl:multiplicationRequest" name="multiplicationRequest">

       </wsdl:input>

         <wsdl:output message="impl:multiplicationResponse" name="multiplicationResponse">

       </wsdl:output>

      </wsdl:operation>

   </wsdl:portType>

   <wsdl:binding name="CalculationServiceImplSoapBinding" type="impl:CalculationServiceImpl">

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

      <wsdl:operation name="sum">

         <wsdlsoap:operation soapAction=""/>

         <wsdl:input name="sumRequest">

            <wsdlsoap:body use="literal"/>

         </wsdl:input>

         <wsdl:output name="sumResponse">

            <wsdlsoap:body use="literal"/>

         </wsdl:output>

      </wsdl:operation>

      <wsdl:operation name="multiplication">

         <wsdlsoap:operation soapAction=""/>

         <wsdl:input name="multiplicationRequest">

            <wsdlsoap:body use="literal"/>

         </wsdl:input>

         <wsdl:output name="multiplicationResponse">

            <wsdlsoap:body use="literal"/>

         </wsdl:output>

      </wsdl:operation>

   </wsdl:binding>

   <wsdl:service name="CalculationServiceImplService">

      <wsdl:port binding="impl:CalculationServiceImplSoapBinding" name="CalculationServiceImpl">

         <wsdlsoap:address location="http://localhost:8080/CalculationService/services/CalculationServiceImpl"/>

      </wsdl:port>

   </wsdl:service>

</wsdl:definitions>

WSDL Document

A WSDL document describes a web service in xml format. It has various elements. It specifies the location of the service, and the methods of the service. Below are the major elements

ElementDescription
<definitions>It is the root element in WSDL
<types>Defines the (XML Schema) data types used by the web service
<message>Defines the data elements for each operation
<portType>Describes the operations that can be performed and the messages involved.
<binding>Defines the protocol and data format for each port type
wsdl elements

Structure of WSDL document

The main structure of WSDL document is as follows

<definitions>

<types>
  definitions of data types used in the service..
</types>

<message>
  definition of the data being communicated..
</message>

<portType>
  set of operations which has the business logic..
</portType>

<binding>
  protocol and data format specifications..
</binding>

</definitions>

The <portType> Element

The <portType> element defines web service operations that can be performed and the messages involved.

Although. request-response type is the most common operation type, but there are more. WSDL defines four types as described below

TypeDefinition
One-wayThe operation can receive a message but will not return a response
Request-responseThe operation can receive a request and will return a response
Solicit-responseThe operation can send a request and will wait for a response
NotificationThe operation can send a message but will not wait for a response

WSDL One-way operation example

<message name="termsAndConditionsApplied">
  <part name="terms" type="xs:string"/>
  <part name="conditions" type="xs:string"/>
</message>

<portType name="policyTermsAndConditions">
  <operation name="showTermsAndConditions">
    <input name="termsAndConditions" message="termsAndConditionsApplied"/>
  </operation>
</portType >

WSDL request/response operation example

This is the most common type. Below is the example

   <wsdl:message name="sumRequest">

      <wsdl:part element="impl:sum" name="parameters">

      </wsdl:part>

   </wsdl:message>

   <wsdl:message name="sumResponse">

      <wsdl:part element="impl:sumResponse" name="parameters">

      </wsdl:part>

   </wsdl:message>


   <wsdl:portType name="CalculationServiceImpl">

      <wsdl:operation name="sum">

         <wsdl:input message="impl:sumRequest" name="sumRequest">

       </wsdl:input>

         <wsdl:output message="impl:sumResponse" name="sumResponse">

       </wsdl:output>

      </wsdl:operation>

   </wsdl:portType>

WSDL Binding to SOAP

WSDL bindings defines the message format and protocol details for a web service.

A request-response operation example:

The two attributes of binding are : name and type.

The name attribute (you can use any name you want) defines the name of the binding. The type attribute points to the port for the binding. in this case the “CalculationServiceImpl” port.

The soap:binding element has two attributes – style and transport.

The style attribute can be “rpc” or “document”. In this case we use document. The transport attribute defines the SOAP protocol to use. In our case we use http.

The operation element defines each operation that is exposed by the portype.

   <wsdl:message name="sumRequest">

      <wsdl:part element="impl:sum" name="parameters">

      </wsdl:part>

   </wsdl:message>

   <wsdl:message name="sumResponse">

      <wsdl:part element="impl:sumResponse" name="parameters">

      </wsdl:part>

   </wsdl:message>


   <wsdl:portType name="CalculationServiceImpl">

      <wsdl:operation name="sum">

         <wsdl:input message="impl:sumRequest" name="sumRequest">

       </wsdl:input>

         <wsdl:output message="impl:sumResponse" name="sumResponse">

       </wsdl:output>

      </wsdl:operation>

   </wsdl:portType>
   
   <wsdl:binding name="CalculationServiceImplSoapBinding" type="impl:CalculationServiceImpl">

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

        <wsdl:operation name="sum">

         <wsdlsoap:operation soapAction=""/>

         <wsdl:input name="sumRequest">

            <wsdlsoap:body use="literal"/>

         </wsdl:input>

         <wsdl:output name="sumResponse">

            <wsdlsoap:body use="literal"/>

         </wsdl:output>

      </wsdl:operation>

   </wsdl:binding>