Web Services Description Language

Web Services Description Language

WSDL (Web Services Description Language) is an XML-based language used to describe the functionalities offered by a web service. It provides a standardized method for describing the operations provided by a service, the messages involved in those operations, and how they are bound to a specific network protocol. WSDL serves as a contract between the service provider and the consumer, allowing them to understand how to interact with the web service without any prior knowledge of its implementation.

Web Services Description Language

Key Components of WSDL:

  • 1.  Types : Defines the data types used by the web service. This is typically specified using XML Schema.
  • 2.  Messages : Describes the input and output messages of the service operations.
  • 3.  PortType : Defines a collection of operations supported by the service.
  • 4.  Binding : Specifies the protocol and data format for each port type.
  • 5.  Service : Defines the endpoint (or address) for the service.

Explanation in Java Example

To illustrate how WSDL is used in Java, we’ll create a simple SOAP web service and generate the WSDL file for it. Then, we’ll create a client to consume the web service using the generated WSDL.

1. Creating the SOAP Web Service
java
import javax.jws.WebMethod;
import javax.jws.WebService;

@WebService
public interface HelloWorld {
    @WebMethod
    String sayHello(String name);
}

Service Implementation
java
import javax.jws.WebService;

@WebService(endpointInterface = "com.example.HelloWorld")
public class HelloWorldImpl implements HelloWorld {
    @Override
    public String sayHello(String name) {
        return "Hello, " + name + "!";
    }
}

2. Publishing the SOAP Web Service
java
import javax.xml.ws.Endpoint;

public class HelloWorldPublisher {
    public static void main(String[] args) {
        Endpoint.publish("http://localhost:8080/ws/hello", new HelloWorldImpl());
        System.out.println("Service is published!");
    }
}

3.  Generating the WSDL File

When the service is published, JAX-WS will automatically generate the WSDL file. You can access it by navigating to http://localhost:8080/ws/hello?wsdl in your web browser. The generated WSDL file will look something like this:

Syntax
xml
<definitions xmlns="http://schemas.xmlsoap.org/wsdl/"
             xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
             xmlns:tns="http://example.com/"
             xmlns:xsd="http://www.w3.org/2001/XMLSchema"
             name="HelloWorldImplService"
             targetNamespace="http://example.com/">
    <types>
        <xsd:schema>
            <xsd:import namespace="http://example.com/" schemaLocation="http://localhost:8080/ws/hello?xsd=1"/>
        </xsd:schema>
    </types>
    <message name="sayHello">
        <part name="parameters" element="tns:sayHello"/>
    </message>
    <message name="sayHelloResponse">
        <part name="parameters" element="tns:sayHelloResponse"/>
    </message>
    <portType name="HelloWorld">
        <operation name="sayHello">
            <input message="tns:sayHello"/>
            <output message="tns:sayHelloResponse"/>
        </operation>
    </portType>
    <binding name="HelloWorldImplPortBinding" type="tns:HelloWorld">
        <soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"/>
        <operation name="sayHello">
            <soap:operation soapAction=""/>
            <input>
                <soap:body use="literal"/>
            </input>
            <output>
                <soap:body use="literal"/>
            </output>
        </operation>
    </binding>
    <service name="HelloWorldImplService">
        <port name="HelloWorldImplPort" binding="tns:HelloWorldImplPortBinding">
            <soap:address location="http://localhost:8080/ws/hello"/>
        </port>
    </service>
</definitions>

4. Creating a SOAP Client Using WSDL
java
import java.net.URL;
import javax.xml.namespace.QName;
import javax.xml.ws.Service;

public class HelloWorldClient {
    public static void main(String[] args) throws Exception {
        URL url = new URL("http://localhost:8080/ws/hello?wsdl");
        QName qname = new QName("http://example.com/", "HelloWorldImplService");
        Service service = Service.create(url, qname);
        HelloWorld hello = service.getPort(HelloWorld.class);
        System.out.println(hello.sayHello("World"));
    }
}

This client uses the WSDL file to understand how to communicate with the web service, illustrating the key role WSDL plays in SOAP web services.