SOAP Web Services

Simple Object Access Protocol (SOAP) is a protocol used for exchanging structured information in the implementation of web services in computer networks. SOAP is based on XML and is platform-independent, which means that it can be used by applications on different operating systems to communicate. SOAP web services provide a way to invoke methods over the web, allowing for seamless interaction between applications written in different languages and running on different platforms.

What is SOAP?

SOAP stands for Simple Object Access Protocol. It is a protocol for exchanging information, especially in web services. SOAP provides the messaging framework that allows applications to send messages over different protocols like HTTP, SMTP, TCP, etc. SOAP is standardized by the World Wide Web Consortium (W3C), which ensures that it is a reliable and well-defined protocol.

Key Features of SOAP:

  • Platform and Language Independent: SOAP can be used across different platforms and programming languages.
  • Protocol-agnostic: Although SOAP is often used with HTTP, it can also work over other protocols such as SMTP.
  • Extensible: It supports extensions such as security (WS-Security) and transactions.
  • Standardized: SOAP has a well-defined structure and is governed by the W3C standards.

SOAP Message Structure

A SOAP message is an XML document with a defined structure. It is composed of the following parts:

  1. Envelope: The root element of the message. It defines the start and the end of the message.
  2. Header (Optional): This is an optional element used to carry information such as security or transaction details.
  3. Body: The main content of the message. It contains the data that is sent to the web service.
  4. Fault (Optional): Used to provide information about errors that occur during the processing of the message.

Example of a Basic SOAP Message:

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

				
			

SOAP Envelope

The SOAP Envelope is the root element in any SOAP message and defines the namespaces and structure of the message.

				
					<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" 
                  xmlns:web="http://example.com/webservice">
    
</soapenv:Envelope>
				
			

The xmlns:soapenv defines the SOAP envelope namespace, while xmlns:web defines a custom namespace for the service.

SOAP Header (Optional)

The SOAP Header is an optional element that can be used to pass additional information such as authentication credentials, transaction data, or security tokens.

				
					<soapenv:Header>
    <web:AuthHeader>
        <web:username>admin</web:username>
        <web:password>secret</web:password>
    </web:AuthHeader>
</soapenv:Header>

				
			

Here, the AuthHeader contains security information (username and password) that might be required by the web service.

SOAP Body

The Body element is the main part of a SOAP message and contains the actual message that is being sent to or received from the web service. The body typically contains request and response information.

Example:

				
					<soapenv:Body>
    <web:AddRequest>
        <web:number1>5</web:number1>
        <web:number2>10</web:number2>
    </web:AddRequest>
</soapenv:Body>

				
			

In this example, the AddRequest operation is being invoked with two numbers (number1 and number2) that are sent to the server for processing.

SOAP Faults (Error Handling)

The Fault element is used to report errors that occur while processing the SOAP message. It is optional but extremely important when something goes wrong.

Example of a SOAP Fault:

				
					<soapenv:Body>
    <soapenv:Fault>
        <faultcode>soapenv:Client</faultcode>
        <faultstring>Invalid request format</faultstring>
    </soapenv:Fault>
</soapenv:Body>
				
			

Here, the fault indicates that the request sent by the client is not in the correct format.

SOAP over HTTP

In most cases, SOAP messages are transmitted over HTTP (Hypertext Transfer Protocol). SOAP works seamlessly with HTTP and can be easily integrated into web applications. When using HTTP, a SOAP message is typically embedded in the body of an HTTP request or response.

Example HTTP SOAP Request:

				
					POST /calculatorService HTTP/1.1
Host: example.com
Content-Type: text/xml; charset=utf-8
Content-Length: 344

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

				
			

Example HTTP SOAP Response:

				
					HTTP/1.1 200 OK
Content-Type: text/xml; charset=utf-8
Content-Length: 512

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

				
			

The SOAP request asks the server to add two numbers (5 and 10), and the SOAP response returns the result (15).

Creating a SOAP Web Service

Creating a SOAP web service typically involves defining the service in WSDL (Web Services Description Language), implementing the business logic, and exposing the service over a network. Below is an example of creating a simple SOAP web service in Java using JAX-WS.

Steps to Create a SOAP Web Service in Java

1. Create the Service Endpoint Interface (SEI)

				
					import javax.jws.WebMethod;
import javax.jws.WebService;

@WebService
public interface CalculatorService {
    @WebMethod
    int add(int number1, int number2);
}
				
			

The @WebService annotation marks the interface as a SOAP web service, and the @WebMethod annotation marks the add method as a web service operation.

2. Implement the Web Service

				
					import javax.jws.WebService;

@WebService(endpointInterface = "com.example.CalculatorService")
public class CalculatorServiceImpl implements CalculatorService {
    public int add(int number1, int number2) {
        return number1 + number2;
    }
}
				
			

The service is implemented by defining the logic for the add method, which adds two numbers.

3. Publish the Web Service

				
					import javax.xml.ws.Endpoint;

public class CalculatorPublisher {
    public static void main(String[] args) {
        Endpoint.publish("http://localhost:8080/calculator", new CalculatorServiceImpl());
        System.out.println("SOAP Web Service is running...");
    }
}
				
			

This code publishes the service at the specified URL (http://localhost:8080/calculator).

4. Accessing the WSDL Once the service is running, you can access its WSDL by navigating to:

				
					http://localhost:8080/calculator?wsdl
				
			

Consuming a SOAP Web Service

To consume a SOAP web service, you can use a tool like wsimport to generate client stubs from the WSDL. Here’s an example of how to consume the service created earlier:

Steps to Consume a SOAP Web Service in Java:

1. Generate Client Stubs

				
					wsimport -keep http://localhost:8080/calculator?wsdl
				
			

2. Create a Client Application

				
					import com.example.CalculatorService;
import com.example.CalculatorServiceImplService;

public class CalculatorClient {
    public static void main(String[] args) {
        CalculatorServiceImplService service = new CalculatorServiceImplService();
        CalculatorService calculator = service.getCalculatorServiceImplPort();
        
        int result = calculator.add(5, 10);
        System.out.println("Result: " + result);
    }
}

				
			

Output:

				
					Result: 15

				
			

This client sends a request to the SOAP web service to add two numbers and prints the result.

Advantages of SOAP Web Services

  • Language and Platform Independence: Since SOAP is XML-based, it can be used in any programming language and on any platform.
  • Protocol Independence: SOAP can be used over a variety of protocols like HTTP, SMTP, and more.
  • Extensibility: SOAP is extensible with additional features such as security (WS-Security), reliability (WS-Reliability), and transactions.
  • Standardization: SOAP is an official protocol maintained by W3C, which ensures that it follows strict standards and guidelines.

SOAP web services offer a robust, platform-independent, and secure way to communicate over the web. Its XML-based structure ensures that it can be used across various platforms and programming languages. Happy Coding!❤️

Table of Contents