Defining Web Services with WSDL

Web Services Description Language (WSDL) is an XML-based language used to describe the functionality, communication protocols, and location of web services. It acts as a contract between the web service provider and the client, detailing how services can be accessed, the operations they offer, and the data they exchange. WSDL plays a vital role in enabling interoperability across different platforms and programming languages, making it essential for defining web services in distributed environments.

Introduction to Web Services and WSDL

What Are Web Services?

Web services are a method of communication between two electronic devices over a network. They allow different applications to talk to each other using standard web protocols. For example, a weather application on your phone can use a web service to fetch real-time weather data from a server.

Why Do We Need Web Services?

Web services are platform-independent and language-neutral, meaning any application—whether it’s built using Java, .NET, or Python—can communicate with another application using web services. They are the backbone of distributed computing and play a crucial role in modern-day web-based architectures like microservices and Service-Oriented Architecture (SOA).

What is WSDL?

WSDL stands for Web Services Description Language. It’s an XML-based language used to describe the functionalities offered by a web service. In simpler terms, WSDL acts like a contract between a web service and the clients who want to use that service. It tells the client what the service does, how to call it, and where it is located.

Basic Structure of WSDL

WSDL documents have a specific structure. They are essentially XML files that define:

  • What a service does: The operations it provides.
  • How to call it: The data formats and protocols used.
  • Where it is located: The URL where the service can be accessed.

A WSDL document is divided into several key components, which we’ll explain in detail.

WSDL Elements Breakdown

The definitions Element

The <definitions> element is the root element of a WSDL file. It contains all the information needed to describe a web service. The targetNamespace attribute is used to specify a unique name for this WSDL document.

Example:

				
					<definitions name="CalculatorService"
             targetNamespace="http://example.com/calculator"
             xmlns="http://schemas.xmlsoap.org/wsdl/"
             xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
             xmlns:xsd="http://www.w3.org/2001/XMLSchema"
             xmlns:tns="http://example.com/calculator">
</definitions>
				
			

Explanation:

  • name: The name of the web service, CalculatorService.
  • targetNamespace: This is a unique URL that identifies the web service.
  • xmlns: These are XML namespaces. They are required to differentiate between different XML vocabularies.

The types Element

The <types> element is used to define the data types used by the web service. It often contains XML Schema (XSD) definitions for complex data types.

Example:

				
					<types>
  <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <xsd:element name="AddRequest">
      <xsd:complexType>
        <xsd:sequence>
          <xsd:element name="number1" type="xsd:int"/>
          <xsd:element name="number2" type="xsd:int"/>
        </xsd:sequence>
      </xsd:complexType>
    </xsd:element>
  </xsd:schema>
</types>

				
			

Explanation:

  • This defines a request, AddRequest, which contains two integer numbers: number1 and number2.

The message Element

The <message> element defines the data being exchanged between the client and the service. Each message can consist of one or more parts, which represent the parameters of the web service.

Example:

				
					<message name="AddRequestMessage">
  <part name="number1" type="xsd:int"/>
  <part name="number2" type="xsd:int"/>
</message>

<message name="AddResponseMessage">
  <part name="result" type="xsd:int"/>
</message>
				
			

Explanation:

  • The AddRequestMessage contains two parts: number1 and number2, both integers.
  • The AddResponseMessage contains one part: result, which is also an integer.

The portType Element

The <portType> element defines the operations provided by the web service. Each operation corresponds to a function or method that the service offers.

Example:

				
					<portType name="CalculatorPortType">
  <operation name="Add">
    <input message="tns:AddRequestMessage"/>
    <output message="tns:AddResponseMessage"/>
  </operation>
</portType>

				
			

Explanation:

  • The Add operation takes an AddRequestMessage as input and returns an AddResponseMessage as output.

The binding Element

The <binding> element specifies the communication protocol (e.g., SOAP) used by the web service.

Example:

				
					<binding name="CalculatorBinding" type="tns:CalculatorPortType">
  <soap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http"/>
  <operation name="Add">
    <soap:operation soapAction="http://example.com/calculator#Add"/>
    <input>
      <soap:body use="encoded" namespace="http://example.com/calculator"/>
    </input>
    <output>
      <soap:body use="encoded" namespace="http://example.com/calculator"/>
    </output>
  </operation>
</binding>

				
			

Explanation:

  • soap : Specifies that SOAP is used for communication, and it’s using the RPC style.
  • soap : Defines the SOAP action and points to the Add operation.

The service Element

The <service> element defines the web service’s endpoint, which is the URL where clients can access the service.

Example:

				
					<service name="CalculatorService">
  <port name="CalculatorPort" binding="tns:CalculatorBinding">
    <soap:address location="http://example.com/calculatorService"/>
  </port>
</service>

				
			

Explanation:

  • service: The name of the service is CalculatorService.
  • port: It binds the service to the CalculatorBinding.
  • soap : The location where the service is hosted (http://example.com/calculatorService).

Working with SOAP and WSDL

WSDL is tightly coupled with SOAP (Simple Object Access Protocol), which is a protocol used for exchanging structured information in web services. SOAP messages are XML-based and are typically transported using HTTP.

SOAP Request Example

A SOAP request to call the Add method might look like this:

				
					<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:cal="http://example.com/calculator">
   <soapenv:Header/>
   <soapenv:Body>
      <cal:AddRequest>
         <cal:number1>5</cal:number1>
         <cal:number2>10</cal:number2>
      </cal:AddRequest>
   </soapenv:Body>
</soapenv:Envelope>

				
			

This XML message is sent from the client to the web service. It calls the Add operation with the numbers 5 and 10.

SOAP Response Example

The server responds with:

				
					<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:cal="http://example.com/calculator">
   <soapenv:Header/>
   <soapenv:Body>
      <cal:AddResponse>
         <cal:result>15</cal:result>
      </cal:AddResponse>
   </soapenv:Body>
</soapenv:Envelope>

				
			

The response contains the result of the Add operation, which is 15.

Advanced Concepts in WSDL

Using Complex Types

In WSDL, you can define complex types to handle more intricate data structures, such as arrays, objects, and custom types.

Example of a complex type:

				
					<types>
  <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <xsd:element name="Person">
      <xsd:complexType>
        <xsd:sequence>
          <xsd:element name="name" type="xsd:string"/>
          <xsd:element name="age" type="xsd:int"/>
        </xsd:sequence>
      </xsd:complexType>
    </xsd:element>
  </xsd:schema>
</types>

				
			

Here, we define a Person type, which has two elements: name (string) and age (integer).

WSDL 2.0 vs WSDL 1.1

WSDL 1.1 is the most commonly used version, but WSDL 2.0 introduces several improvements, such as better HTTP binding support and asynchronous messaging.

Complete WSDL Example

Here’s a full WSDL example for a simple calculator web service:

				
					<definitions name="CalculatorService"
             targetNamespace="http://example.com/calculator"
             xmlns="http://schemas.xmlsoap.org/wsdl/"
             xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
             xmlns:xsd="http://www.w3.org/2001/XMLSchema"
             xmlns:tns="http://example.com/calculator">
  
  <types>
    <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
      <xsd:element name="AddRequest">
        <xsd:complexType>
          <xsd:sequence>
            <xsd:element name="number1" type="xsd:int"/>
            <xsd:element name="number2" type="xsd:int"/>
          </xsd:sequence>
        </xsd:complexType>
      </xsd:element>
    </xsd:schema>
  </types>

  <message name="AddRequestMessage">
    <part name="number1" type="xsd:int"/>
    <part name="number2" type="xsd:int"/>
  </message>

  <message name="AddResponseMessage">
    <part name="result" type="xsd:int"/>
  </message>

  <portType name="CalculatorPortType">
    <operation name="Add">
      <input message="tns:AddRequestMessage"/>
      <output message="tns:AddResponseMessage"/>
    </operation>
  </portType>

  <binding name="CalculatorBinding" type="tns:CalculatorPortType">
    <soap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http"/>
    <operation name="Add">
      <soap:operation soapAction="http://example.com/calculator#Add"/>
      <input>
        <soap:body use="encoded" namespace="http://example.com/calculator"/>
      </input>
      <output>
        <soap:body use="encoded" namespace="http://example.com/calculator"/>
      </output>
    </operation>
  </binding>

  <service name="CalculatorService">
    <port name="CalculatorPort" binding="tns:CalculatorBinding">
      <soap:address location="http://example.com/calculatorService"/>
    </port>
  </service>
  
</definitions>

				
			

WSDL is an essential part of web services as it provides a standardized way to describe the functionalities, protocols, and data structures of a service. From the basic to the advanced use of complex types and operations, WSDL allows seamless communication between distributed systems. By understanding the structure and functionality of WSDL, you can define and use web services efficiently. Happy Coding!❤️

Table of Contents

Contact here

Copyright © 2025 Diginode

Made with ❤️ in India