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
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.
以上是如何使用 Java 中的 SAAJ 建立一個可用的 SOAP 用戶端?的詳細內容。更多資訊請關注PHP中文網其他相關文章!