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.
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.
A SOAP message is an XML document with a defined structure. It is composed of the following parts:
5
10
The SOAP Envelope is the root element in any SOAP message and defines the namespaces and structure of the message.
The xmlns:soapenv
defines the SOAP envelope namespace, while xmlns:web
defines a custom namespace for the service.
The SOAP Header is an optional element that can be used to pass additional information such as authentication credentials, transaction data, or security tokens.
admin
secret
Here, the AuthHeader
contains security information (username and password) that might be required by the web service.
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.
5
10
In this example, the AddRequest
operation is being invoked with two numbers (number1
and number2
) that are sent to the server for processing.
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.
soapenv:Client
Invalid request format
Here, the fault indicates that the request sent by the client is not in the correct format.
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.
POST /calculatorService HTTP/1.1
Host: example.com
Content-Type: text/xml; charset=utf-8
Content-Length: 344
5
10
HTTP/1.1 200 OK
Content-Type: text/xml; charset=utf-8
Content-Length: 512
15
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 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.
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
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:
1. Generate Client Stubs
wsimport -keep http://localhost:8080/calculator?wsdl
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);
}
}
Result: 15
This client sends a request to the SOAP web service to add two numbers and prints the result.
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!❤️