Home > Java > javaTutorial > How to Implement a SOAP Client in Java using SAAJ?

How to Implement a SOAP Client in Java using SAAJ?

DDD
Release: 2024-11-08 22:54:02
Original
474 people have browsed it

How to Implement a SOAP Client in Java using SAAJ?

Working SOAP Client Example

Implementing SOAP clients in Java can be simplified by utilizing the SAAJ framework. This versatile framework enables developers to handle SOAP request and response messages directly, surpassing the functionalities of JAX-WS.

To illustrate the functionality of SAAJ, consider the following web service call:

import javax.xml.soap.*;

public class SOAPClientSAAJ {

    public static void main(String[] args) {
        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 {
        SOAPPart soapPart = soapMessage.getSOAPPart();
        String myNamespace = "myNamespace";
        String myNamespaceURI = "http://www.webserviceX.NET";

        SOAPEnvelope envelope = soapPart.getEnvelope();
        envelope.addNamespaceDeclaration(myNamespace, myNamespaceURI);

        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 {
            SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance();
            SOAPConnection soapConnection = soapConnectionFactory.createConnection();

            SOAPMessage soapResponse = soapConnection.call(createSOAPRequest(soapAction), soapEndpointUrl);
            soapResponse.writeTo(System.out);
            System.out.println();

            soapConnection.close();
        } catch (Exception e) {
            System.err.println("Error sending SOAP Request. Ensure correct endpoint URL and SOAPAction.");
            e.printStackTrace();
        }
    }

    private static SOAPMessage createSOAPRequest(String soapAction) throws Exception {
        MessageFactory messageFactory = MessageFactory.newInstance();
        SOAPMessage soapMessage = messageFactory.createMessage();

        createSoapEnvelope(soapMessage);
        MimeHeaders headers = soapMessage.getMimeHeaders();
        headers.addHeader("SOAPAction", soapAction);
        soapMessage.saveChanges();

        soapMessage.writeTo(System.out);
        System.out.println();

        return soapMessage;
    }

}
Copy after login

This comprehensive example demonstrates a SOAP web service call using SAAJ. Modify the parameters to suit your specific web service requirements, and remember to alter the contents of the createSoapEnvelope() method to customize the SOAP envelope's content.

The above is the detailed content of How to Implement a SOAP Client in Java using SAAJ?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template