How can you create a working SOAP client using SAAJ in Java?
Working SOAP Client Example
Introduction
SOAP is a widely used protocol for exchanging XML-based messages over the web. Implementing a SOAP client can be a challenging task, especially for beginners. This article aims to provide a simple and practical example of a working SOAP client in Java, utilizing the SAAJ (SOAP with Attachments API for Java) framework.
SAAJ: SOAP with Attachments API for Java
SAAJ is a framework within Java for handling SOAP messages directly. It enables developers to create and parse SOAP messages without using JAX-WS. SAAJ provides a simplified interface for working with SOAP messages, making it an ideal choice for creating SOAP clients.
Working SOAP Client Example
The following code snippet showcases a working SOAP client example using SAAJ. This client calls a web service to retrieve information about a specific city:
import javax.xml.soap.*; public class SOAPClientSAAJ { // SAAJ - SOAP Client Testing public static void main(String args[]) { // SOAP Endpoint URL and SOAP Action String soapEndpointUrl = "http://www.webservicex.net/uszip.asmx"; String soapAction = "http://www.webserviceX.NET/GetInfoByCity"; callSoapWebService(soapEndpointUrl, soapAction); } private static void createSoapEnvelope(SOAPMessage soapMessage) throws SOAPException { // Create SOAP Envelope and Namespace SOAPPart soapPart = soapMessage.getSOAPPart(); String myNamespace = "myNamespace"; String myNamespaceURI = "http://www.webserviceX.NET"; SOAPEnvelope envelope = soapPart.getEnvelope(); envelope.addNamespaceDeclaration(myNamespace, myNamespaceURI); // Create SOAP Body and Request Content SOAPBody soapBody = envelope.getBody(); SOAPElement soapBodyElem = soapBody.addChildElement("GetInfoByCity", myNamespace); SOAPElement soapBodyElem1 = soapBodyElem.addChildElement("USCity", myNamespace); soapBodyElem1.addTextNode("New York"); } private static void callSoapWebService(String soapEndpointUrl, String soapAction) { try { // Create SOAP Connection and Message SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance(); SOAPConnection soapConnection = soapConnectionFactory.createConnection(); SOAPMessage soapMessage = createSOAPRequest(soapAction); // Send SOAP Message and Receive Response SOAPMessage soapResponse = soapConnection.call(soapMessage, soapEndpointUrl); soapResponse.writeTo(System.out); soapConnection.close(); } catch (Exception e) { System.err.println("Error sending SOAP Request!"); e.printStackTrace(); } } private static SOAPMessage createSOAPRequest(String soapAction) throws Exception { // Create SOAP Message and Add Headers MessageFactory messageFactory = MessageFactory.newInstance(); SOAPMessage soapMessage = messageFactory.createMessage(); createSoapEnvelope(soapMessage); MimeHeaders headers = soapMessage.getMimeHeaders(); headers.addHeader("SOAPAction", soapAction); soapMessage.saveChanges(); return soapMessage; } }
Running the Example
To execute this example, you will need to have Java installed on your system. Save the code snippet as a file with a .java extension, compile it using javac, and then run it with java. The code will call the web service to retrieve information about the city "New York", and the response will be printed on the console.
Additional Notes
- This example uses a simple web service for demonstration purposes; you can modify it to call other SOAP web services.
- SAAJ was removed from Java 11, so you may need to use an alternative framework or library if using a newer version of Java.
By following this example and understanding the concepts of SOAP message construction and handling using SAAJ, you can confidently build SOAP clients for your own applications.
The above is the detailed content of How can you create a working SOAP client using SAAJ in Java?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Troubleshooting and solutions to the company's security software that causes some applications to not function properly. Many companies will deploy security software in order to ensure internal network security. ...

Field mapping processing in system docking often encounters a difficult problem when performing system docking: how to effectively map the interface fields of system A...

When using MyBatis-Plus or other ORM frameworks for database operations, it is often necessary to construct query conditions based on the attribute name of the entity class. If you manually every time...

Solutions to convert names to numbers to implement sorting In many application scenarios, users may need to sort in groups, especially in one...

Start Spring using IntelliJIDEAUltimate version...

Conversion of Java Objects and Arrays: In-depth discussion of the risks and correct methods of cast type conversion Many Java beginners will encounter the conversion of an object into an array...

Detailed explanation of the design of SKU and SPU tables on e-commerce platforms This article will discuss the database design issues of SKU and SPU in e-commerce platforms, especially how to deal with user-defined sales...

When using TKMyBatis for database queries, how to gracefully get entity class variable names to build query conditions is a common problem. This article will pin...
