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.
WSDL is an XML-based language for describing the network services provided by a web service. It defines:
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.
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:
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.
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.
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.
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.
binding
The <binding>
element specifies the protocol and data format used to communicate with the web service, typically SOAP or HTTP.
service
The <service>
element specifies the endpoint (location) where the service can be accessed. This includes the URL of the web service.
Here’s an example of a basic WSDL structure.
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.
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.
message
Messages are the data that is exchanged between the service and its clients. The <message>
element defines the structure of the data.
portType
This element defines the operations provided by the service. Each <operation>
defines an action that the client can invoke.
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.
service
This element defines the service endpoint, specifying where the service is hosted and how it can be accessed.
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.
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.
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).
wsimport
tool from the WSDL URL:
wsimport -keep http://example.com/calculatorService?wsdl
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);
}
}
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!❤️