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!

The article discusses implementing multi-level caching in Java using Caffeine and Guava Cache to enhance application performance. It covers setup, integration, and performance benefits, along with configuration and eviction policy management best pra

This article explores integrating functional programming into Java using lambda expressions, Streams API, method references, and Optional. It highlights benefits like improved code readability and maintainability through conciseness and immutability

Java's classloading involves loading, linking, and initializing classes using a hierarchical system with Bootstrap, Extension, and Application classloaders. The parent delegation model ensures core classes are loaded first, affecting custom class loa

The article discusses using JPA for object-relational mapping with advanced features like caching and lazy loading. It covers setup, entity mapping, and best practices for optimizing performance while highlighting potential pitfalls.[159 characters]

The article discusses using Maven and Gradle for Java project management, build automation, and dependency resolution, comparing their approaches and optimization strategies.

This article explains Java's NIO API for non-blocking I/O, using Selectors and Channels to handle multiple connections efficiently with a single thread. It details the process, benefits (scalability, performance), and potential pitfalls (complexity,

The article discusses creating and using custom Java libraries (JAR files) with proper versioning and dependency management, using tools like Maven and Gradle.

This article details Java's socket API for network communication, covering client-server setup, data handling, and crucial considerations like resource management, error handling, and security. It also explores performance optimization techniques, i


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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 English version
Recommended: Win version, supports code prompts!

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

Zend Studio 13.0.1
Powerful PHP integrated development environment