首页 > Java > java教程 > 正文

如何使用 Java 中的 SAAJ 创建一个可用的 SOAP 客户端?

DDD
发布: 2024-11-13 11:24:02
原创
778 人浏览过

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.

以上是如何使用 Java 中的 SAAJ 创建一个可用的 SOAP 客户端?的详细内容。更多信息请关注PHP中文网其他相关文章!

来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板