Table of contents
1.
Introduction
2.
SOAP Web Services
2.1.
How SOAP Works
2.2.
SOAP Request
2.3.
SOAP Response
2.4.
Key Components of SOAP
2.5.
Why Use SOAP?
3.
Message Format
4.
Creating a SOAP Web Service in Java
4.1.
Step 1: Define the Service Endpoint Interface (SEI)
4.2.
Step 2: Implement the Service
4.3.
Step 3: Publish the Service
4.4.
Step 4: Test the Service
5.
Example of a SOAP Message Format
6.
Breakdown of the Message
7.
Sample Message
7.1.
SOAP Request
7.2.
SOAP Response
8.
Advantages of SOAP
9.
Disadvantages of SOAP Web Services
10.
Frequently Asked Questions
10.1.
What is SOAP used for?
10.2.
How does SOAP differ from REST?
10.3.
Can SOAP be used with JSON?
11.
Conclusion
Last Updated: Jan 12, 2025
Medium

SOAP Web Services

Author Rahul Singh
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

SOAP (Simple Object Access Protocol) Web Services are a way for applications to communicate with each other over the internet. They use XML for sending and receiving messages, making them platform-independent and language-neutral. SOAP Web Services follow strict standards, which ensure reliable and secure data exchange between systems, even if they are built using different technologies.

SOAP Web Services

In this article, we will discuss SOAP message format, see a sample SOAP message, and discuss the advantages of using SOAP web services. 

SOAP Web Services

SOAP web services are a way for applications to communicate over the internet using XML-based messages. These services are platform-independent, meaning they can work across different operating systems & programming languages. SOAP is often used in scenarios where security & reliability are important, such as in banking or healthcare systems.

How SOAP Works

SOAP works by sending XML messages between a client & a server. The client sends a request in XML format, & the server responds with another XML message. These messages are structured in a specific way, following the SOAP protocol. The structure includes an envelope, a header, & a body.

Let’s take a basic example of a SOAP request & response:

SOAP Request

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:web="http://www.example.com/webservice">
   <soapenv:Header/>
   <soapenv:Body>
      <web:GetUserDetails>
         <web:UserID>123</web:UserID>
      </web:GetUserDetails>
   </soapenv:Body>
</soapenv:Envelope>

SOAP Response

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
   <soapenv:Header/>
   <soapenv:Body>
      <web:GetUserDetailsResponse xmlns:web="http://www.example.com/webservice">
         <web:UserDetails>
            <web:Name>John Doe</web:Name>
            <web:Email>john.doe@example.com</web:Email>
         </web:UserDetails>
      </web:GetUserDetailsResponse>
   </soapenv:Body>
</soapenv:Envelope>


In this example, the client requests user details by sending a `UserID` to the server. The server responds with the user’s name & email.

Key Components of SOAP

1. Envelope: The root element of a SOAP message. It defines the start & end of the message.
 

2. Header: Contains optional information like authentication details or transaction IDs.
 

3. Body: The main part of the message, containing the actual request or response data.
 

4. Fault: Used to report errors during message processing.

Why Use SOAP?

SOAP is very useful in many situations like:

Security is critical (it supports WS-Security for encryption & authentication).

Transactions need to be reliable (it supports WS-ReliableMessaging).

Applications are built on different platforms & need to communicate seamlessly.

Message Format

SOAP messages are structured in XML and follow a strict format that ensures interoperability between different systems. The message has the following main parts:

  1. Envelope: Defines the start and end of the message and is mandatory.
     
  2. Header: Contains optional attributes of the message like authentication and transaction details.
     
  3. Body: Contains the main message content and is mandatory.
     
  4. Fault: An optional part used to convey error information.

Creating a SOAP Web Service in Java

Let’s create a simple SOAP web service using Java & the JAX-WS API. This example will show how to create a service that returns user details.

Step 1: Define the Service Endpoint Interface (SEI)

import javax.jws.WebMethod;
import javax.jws.WebService;
@WebService
public interface UserService {
    @WebMethod
    String getUserDetails(int userID);
}

Step 2: Implement the Service

import javax.jws.WebService;


@WebService(endpointInterface = "UserService")
public class UserServiceImpl implements UserService {
    @Override
    public String getUserDetails(int userID) {
        // Simulate fetching user details from a database
        if (userID == 123) {
            return "Name: John Doe, Email: john.doe@example.com";
        } else {
            return "User not found";
        }
    }
}

Step 3: Publish the Service

import javax.xml.ws.Endpoint;


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

Step 4: Test the Service

You can test the service using a tool like Postman or SOAPUI. Send a SOAP request to `http://localhost:8080/ws/user` with the following body:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:web="http://userService/">
   <soapenv:Header/>
   <soapenv:Body>
      <web:getUserDetails>
         <userID>123</userID>
      </web:getUserDetails>
   </soapenv:Body>
</soapenv:Envelope>


The server will respond with the user details in XML format.

Example of a SOAP Message Format

Here is an example of a SOAP message:

<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope">
    <soap:Header>
        <auth:Authentication xmlns:auth="http://example.com/auth">
            <auth:Token>12345</auth:Token>
        </auth:Authentication>
    </soap:Header>
    <soap:Body>
        <m:GetStudentDetails xmlns:m="http://example.com/student">
            <m:StudentID>1001</m:StudentID>
        </m:GetStudentDetails>
    </soap:Body>
</soap:Envelope>

Breakdown of the Message

  1. Envelope: The root element of the message.
     
  2. Header: Includes authentication details, like a token.
     
  3. Body: Requests the details of a student with a specific ID.
     

When this message is processed, the server interprets the StudentID and returns the corresponding details.

Sample Message

SOAP Request

Here’s a SOAP request to get the weather forecast:

<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope">
    <soap:Body>
        <GetWeather xmlns="http://www.weatherapi.com">
            <City>New York</City>
        </GetWeather>
    </soap:Body>
</soap:Envelope>

SOAP Response

Below is the expected response:

<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope">
    <soap:Body>
        <GetWeatherResponse xmlns="http://www.weatherapi.com">
            <City>New York</City>
            <Temperature>15°C</Temperature>
            <Condition>Cloudy</Condition>
        </GetWeatherResponse>
    </soap:Body>
</soap:Envelope>


Explanation

  • Request: The client sends a request with the city name.
     
  • Response: The server returns the temperature and weather condition of the requested city.

Advantages of SOAP

SOAP offers several benefits that make it a reliable choice for web services:

1. Platform Independence

SOAP works across different platforms and programming languages. A message created in one language can be read and processed in another.

2. Extensibility

The protocol supports additional features like security, routing, and transactions via headers, making it highly extensible.

3. Reliability

SOAP uses standardized error handling through the Fault element, ensuring reliable communication.

4. Transport Independence

It can be used over various transport protocols like HTTP, SMTP, or even JMS (Java Messaging Service).

5. Security

With WS-Security, SOAP supports secure message transmission, including encryption and signature verification.

6. Built-in Error Handling

The Fault element provides detailed error messages, making debugging easier.

Disadvantages of SOAP Web Services


1. Complexity

SOAP is known for its complexity. The protocol has strict standards & requires a lot of configuration. Writing WSDL files, handling XML schemas, & managing SOAP envelopes can be overwhelming, especially for beginners or small projects.


2. Heavyweight

SOAP messages are larger in size because they are XML-based. This makes them slower to transmit & process compared to lightweight formats like JSON used in REST. The additional overhead can impact performance, especially in low-bandwidth environments.


3. Slower Performance

Due to the verbose nature of XML & the additional processing required for parsing & validating SOAP messages, SOAP web services tend to be slower than RESTful services. This can be a significant drawback for applications requiring high-speed communication.


4. Limited Browser Support

SOAP is not natively supported by browsers, making it less suitable for web-based applications. Unlike REST, which can easily be consumed by JavaScript, SOAP requires additional libraries or tools to work with browsers.


5. Steeper Learning Curve

SOAP involves a steep learning curve due to its reliance on XML, WSDL, & other standards. Developers need to understand these technologies thoroughly to implement & maintain SOAP services, which can be time-consuming.


6. Less Flexibility

SOAP is rigid in its structure & standards. It doesn’t allow much flexibility in terms of message format or communication style. This can be a limitation when working with modern, dynamic applications that require more adaptability.

Frequently Asked Questions

What is SOAP used for?

SOAP is used to enable communication between applications, especially in distributed environments, using XML-based messages.

How does SOAP differ from REST?

SOAP is a protocol with strict standards, whereas REST is an architectural style that uses standard HTTP methods. SOAP is more suitable for enterprise-level services that require high security and reliability.

Can SOAP be used with JSON?

No, SOAP strictly uses XML for message formatting. However, you can convert SOAP responses into JSON using additional tools or libraries.

Conclusion

In this article, we discussed SOAP web services, focusing on its message format, practical examples, and advantages. SOAP is a robust protocol that ensures secure and reliable communication between applications. By understanding its components and structure, you are better prepared to work with SOAP services in real-world applications. 

You can also check out our other blogs on Code360.

Live masterclass