Java > Java Networking > HTTP and Web Services > SOAP vs REST APIs
Java SOAP Client Example
This snippet demonstrates how to consume a SOAP web service using Java. It uses the `javax.xml.soap` package, which is part of the standard Java API (JAX-WS). It showcases sending a SOAP request and printing the SOAP response.
Code Snippet
This code performs the following steps:
import javax.xml.soap.*;
import javax.xml.namespace.QName;
public class SoapClientExample {
public static void main(String[] args) throws Exception {
// SOAP Endpoint URL
String url = "http://www.dneonline.com/calculator.asmx";
String soapAction = "http://tempuri.org/Add";
// Create SOAP Connection
SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance();
SOAPConnection soapConnection = soapConnectionFactory.createConnection();
// Create SOAP Message
MessageFactory messageFactory = MessageFactory.newInstance();
SOAPMessage soapMessage = messageFactory.createMessage();
// Create SOAP Envelope
SOAPPart soapPart = soapMessage.getSOAPPart();
SOAPEnvelope envelope = soapPart.getEnvelope();
envelope.addNamespaceDeclaration("tem", "http://tempuri.org/");
// Create SOAP Body
SOAPBody soapBody = envelope.getBody();
SOAPElement add = soapBody.addChildElement("Add", "tem");
SOAPElement intA = add.addChildElement("intA", "tem");
intA.addTextNode("5");
SOAPElement intB = add.addChildElement("intB", "tem");
intB.addTextNode("3");
soapMessage.saveChanges();
/* Print the request message */
System.out.print("Request SOAP Message = ");
soapMessage.writeTo(System.out);
System.out.println();
// Send SOAP Message to SOAP Server
SOAPMessage soapResponse = soapConnection.call(soapMessage, url);
// Print the SOAP Response
System.out.print("Response SOAP Message = ");
soapResponse.writeTo(System.out);
System.out.println();
// Extract data from SOAP Response
SOAPBody responseBody = soapResponse.getSOAPBody();
QName returnName = new QName("http://tempuri.org/", "AddResult");
SOAPElement addResult = (SOAPElement) responseBody.getChildElements(returnName).next();
String result = addResult.getValue();
System.out.println("Result: " + result);
soapConnection.close();
}
}
Concepts Behind the Snippet
This snippet illustrates key concepts in SOAP web service consumption:
Real-Life Use Case
SOAP is often used in enterprise environments for integrating legacy systems or when strong security and reliability are required. For example, financial institutions might use SOAP for transaction processing or healthcare providers might use it for exchanging patient information.
Best Practices
Interview Tip
When discussing SOAP, be prepared to explain the differences between SOAP and REST, the role of WSDL, and the structure of a SOAP message (Envelope, Header, Body). Also, be ready to discuss the advantages and disadvantages of SOAP compared to REST.
When to use them
SOAP is a better choice when you need guaranteed message delivery, strong security, transaction support, and formal contracts (WSDL). However, it's more complex and resource-intensive than REST.
Alternatives
Pros
Cons
FAQ
-
What is a WSDL file?
A WSDL (Web Services Description Language) file is an XML document that describes a web service. It specifies the service's operations, input and output parameters, and the location of the service. -
Do I need a WSDL file to use a SOAP service?
While not strictly mandatory in all cases, it's highly recommended. The WSDL provides a contract, making it easier to generate client code and understand the service's capabilities. Without a WSDL, you'll need to manually construct the SOAP requests and parse the responses, which can be error-prone. -
How can I generate client code from a WSDL file?
You can use tools like `wsimport` (part of the JDK) or Apache CXF to generate Java classes from a WSDL file. These tools will create the necessary classes for sending SOAP requests and processing SOAP responses.