Home >Java >javaTutorial >How can you create a working SOAP client using SAAJ in Java?

How can you create a working SOAP client using SAAJ in Java?

DDD
DDDOriginal
2024-11-13 11:24:02836browse

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn