WSDL Elements and Definitions

Web Services Description Language (WSDL) is an XML-based language that describes web services and how to interact with them. WSDL provides a formal contract between a service provider and a service consumer, defining the operations, message formats, transport protocols, and the service endpoint location.

What is WSDL?

WSDL is an XML-based language for describing the network services provided by a web service. It defines:

  • What the service does (operations).
  • How the service is accessed (data format).
  • Where the service is located (endpoint).

WSDL is essential for defining web services, especially when working with SOAP-based services. It provides a language-agnostic way to describe the service so that clients and servers written in different languages can communicate with each other.

Key Features of WSDL:

  • Platform-independent and language-neutral.
  • Supports remote procedure calls (RPCs) and document-oriented services.
  • Describes how the service is called, the input/output data formats, and where it’s located.

Structure of a WSDL Document

A WSDL document is an XML document that contains several elements. Each element plays a critical role in describing the service. The basic structure includes:

a. definitions

This is the root element of a WSDL document. It encloses all other elements and provides basic metadata, such as the target namespace and service name.

b. types

This element defines the data types used by the web service. These are usually XML Schema Definitions (XSDs) that specify the structure of the input and output messages.

c. message

The <message> element defines the actual messages exchanged between the client and server. Each message can contain one or more parts, which describe the parameters (input or output) for the operation.

d. portType

The <portType> element defines a collection of operations that the web service supports. Each operation corresponds to a specific function or method of the service.

e. binding

The <binding> element specifies the protocol and data format used to communicate with the web service, typically SOAP or HTTP.

f. service

The <service> element specifies the endpoint (location) where the service can be accessed. This includes the URL of the web service.

Example:

Here’s an example of a basic WSDL structure.

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

  <types>
    <xsd:schema targetNamespace="http://example.com/calculator">
      <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>

				
			

Detailed Explanation of WSDL Elements

a. definitions

The <definitions> element is the container for all the WSDL elements. It defines the name of the service and specifies the target namespace to avoid naming conflicts.

				
					<definitions name="CalculatorService" targetNamespace="http://example.com/calculator" 
    xmlns="http://schemas.xmlsoap.org/wsdl/">

				
			

b. types

This section is used to define the complex types for the service. These types can be XML Schema-based or simple types like integers and strings.

				
					<types>
  <xsd:schema targetNamespace="http://example.com/calculator">
    <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>
				
			

c. message

Messages are the data that is exchanged between the service and its clients. The <message> element defines the structure of the data.

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

d. portType

This element defines the operations provided by the service. Each <operation> defines an action that the client can invoke.

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

				
			

e. binding

Binding defines how the service is invoked, specifying the transport protocol (e.g., HTTP) and message format (e.g., SOAP). It allows multiple bindings for the same portType, enabling support for different protocols.

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

				
			

f. service

This element defines the service endpoint, specifying where the service is hosted and how it can be accessed.

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

Advanced WSDL Concepts

Document vs. RPC Style

  • RPC Style: In RPC (Remote Procedure Call) style, the WSDL operations are like function calls where the client sends a method name and parameters.

  • Document Style: In Document style, the WSDL operations exchange entire XML documents rather than method parameters.

Example comparison:

				
					
<binding name="CalculatorBinding" type="tns:CalculatorPortType">
  <soap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http"/>
</binding>


<binding name="CalculatorBinding" type="tns:CalculatorPortType">
  <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
</binding>

				
			

One-Way and Two-Way Operations

WSDL supports one-way (fire-and-forget) and two-way (request-response) operations. In one-way operations, the client sends a message without expecting a response. In two-way operations, the client sends a request and expects a response.

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

				
			

Code Example: Consuming a WSDL in Java

Using WSDL, you can auto-generate client stubs to call web services. Here’s an example of how you can consume a WSDL in Java using JAX-WS (Java API for XML Web Services).

1. First, generate the client using the wsimport tool from the WSDL URL:

				
					wsimport -keep http://example.com/calculatorService?wsdl

				
			

2. Next, create the Java client:

				
					import example.com.calculator.CalculatorService;

public class CalculatorClient {
    public static void main(String[] args) {
        CalculatorService service = new CalculatorService();
        Calculator port = service.getCalculatorPort();
        int result = port.add(10, 20);
        System.out.println("Addition Result: " + result);
    }
}

				
			

Output:

				
					Addition Result: 30
				
			

WSDL is a powerful tool for defining web services in a platform-independent way. It allows clients and servers to communicate, regardless of their implementation, by defining the contract for services. Happy Coding!❤️

Table of Contents