调用 SOAP - Web 服务的 Java 类

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5092371/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 09:22:55  来源:igfitidea点击:

Java class to call a SOAP - Web service

javaweb-servicesclasssoap

提问by Mousarules

I have a SOAP that i need to call from Oracle and i have heard that the only way to work it out is through a Java class , Unfortunately im not familiar with Java as i'm an Oracle developer ( Oracle Forms ) I really appreciated it if someone can help me creating a class calling this SOAP so that i can build it on my Oracle database and call it from Oracle forms builder the way i call a function .

我有一个 SOAP 需要从 Oracle 调用,我听说解决它的唯一方法是通过 Java 类,不幸的是我不熟悉 Java,因为我是 Oracle 开发人员(Oracle Forms),我真的很感激如果有人可以帮助我创建一个调用此 SOAP 的类,以便我可以在我的 Oracle 数据库上构建它,并以我调用函数的方式从 Oracle 表单构建器中调用它。

There are two SOAPs (1.1 nd 1.2 ) , both of any can work :

有两个 SOAP(1.1 和 1.2),任何一个都可以工作:

*SOAP 1.1

*肥皂 1.1

The following is a sample SOAP 1.1 request and response. The placeholders shown need to be replaced with actual values.

下面是一个示例 SOAP 1.1 请求和响应。显示的占位符需要替换为实际值。

POST /gmgwebservice/service.asmx HTTP/1.1
Host: 212.35.66.180
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://tempuri.org/SendSMS"

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <SendSMS xmlns="http://tempuri.org/">
      <UserName>string</UserName>
      <Password>string</Password>
      <MessageBody>string</MessageBody>
      <Sender>string</Sender>
      <Destination>string</Destination>
    </SendSMS>
  </soap:Body>
</soap:Envelope>
HTTP/1.1 200 OK
Content-Type: text/xml; charset=utf-8
Content-Length: length

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <SendSMSResponse xmlns="http://tempuri.org/">
      <SendSMSResult>string</SendSMSResult>
    </SendSMSResponse>
  </soap:Body>
</soap:Envelope>

**SOAP 1.2

**肥皂 1.2

The following is a sample SOAP 1.2 request and response. The placeholders shown need to be replaced with actual values.

下面是一个示例 SOAP 1.2 请求和响应。显示的占位符需要替换为实际值。

POST /gmgwebservice/service.asmx HTTP/1.1
Host: 212.35.66.180
Content-Type: application/soap+xml; charset=utf-8
Content-Length: length

<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
  <soap12:Body>
    <SendSMS xmlns="http://tempuri.org/">
      <UserName>string</UserName>
      <Password>string</Password>
      <MessageBody>string</MessageBody>
      <Sender>string</Sender>
      <Destination>string</Destination>
    </SendSMS>
  </soap12:Body>
</soap12:Envelope>
HTTP/1.1 200 OK
Content-Type: application/soap+xml; charset=utf-8
Content-Length: length

<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
  <soap12:Body>
    <SendSMSResponse xmlns="http://tempuri.org/">
      <SendSMSResult>string</SendSMSResult>
    </SendSMSResponse>
  </soap12:Body>
</soap12:Envelope>

回答by acdcjunior

To implement simple SOAP clients in Java, you can use the SAAJ framework (it is shipped with JSE 1.6 and above):

要在 Java 中实现简单的 SOAP 客户端,您可以使用 SAAJ 框架(它随 JSE 1.6 及更高版本一起提供):

SOAP with Attachments API for Java (SAAJ)is mainly used for dealing directly with SOAP Request/Response messages which happens behind the scenes in any Web Service API. It allows the developers to directly send and receive soap messages instead of using JAX-WS.

SOAP with Attachments API for Java (SAAJ)主要用于直接处理发生在任何 Web 服务 API 的幕后的 SOAP 请求/响应消息。它允许开发人员直接发送和接收soap 消息,而不是使用JAX-WS。

See below a working example (run it!) of a SOAP web service call using SAAJ. It calls this web service.

请参阅下面的使用 SAAJ 的 SOAP Web 服务调用的工作示例(运行它!)。它调用此 Web 服务

import javax.xml.soap.*;
import javax.xml.transform.*;
import javax.xml.transform.stream.*;

public class SOAPClientSAAJ {

    /**
     * Starting point for the SAAJ - SOAP Client Testing
     */
    public static void main(String args[]) {
        try {
            // Create SOAP Connection
            SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance();
            SOAPConnection soapConnection = soapConnectionFactory.createConnection();

            // Send SOAP Message to SOAP Server
            String url = "http://ws.cdyne.com/emailverify/Emailvernotestemail.asmx";
            SOAPMessage soapResponse = soapConnection.call(createSOAPRequest(), url);

            // Process the SOAP Response
            printSOAPResponse(soapResponse);

            soapConnection.close();
        } catch (Exception e) {
            System.err.println("Error occurred while sending SOAP Request to Server");
            e.printStackTrace();
        }
    }

    private static SOAPMessage createSOAPRequest() throws Exception {
        MessageFactory messageFactory = MessageFactory.newInstance();
        SOAPMessage soapMessage = messageFactory.createMessage();
        SOAPPart soapPart = soapMessage.getSOAPPart();

        String serverURI = "http://ws.cdyne.com/";

        // SOAP Envelope
        SOAPEnvelope envelope = soapPart.getEnvelope();
        envelope.addNamespaceDeclaration("example", serverURI);

        /*
        Constructed SOAP Request Message:
        <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:example="http://ws.cdyne.com/">
            <SOAP-ENV:Header/>
            <SOAP-ENV:Body>
                <example:VerifyEmail>
                    <example:email>[email protected]</example:email>
                    <example:LicenseKey>123</example:LicenseKey>
                </example:VerifyEmail>
            </SOAP-ENV:Body>
        </SOAP-ENV:Envelope>
         */

        // SOAP Body
        SOAPBody soapBody = envelope.getBody();
        SOAPElement soapBodyElem = soapBody.addChildElement("VerifyEmail", "example");
        SOAPElement soapBodyElem1 = soapBodyElem.addChildElement("email", "example");
        soapBodyElem1.addTextNode("[email protected]");
        SOAPElement soapBodyElem2 = soapBodyElem.addChildElement("LicenseKey", "example");
        soapBodyElem2.addTextNode("123");

        MimeHeaders headers = soapMessage.getMimeHeaders();
        headers.addHeader("SOAPAction", serverURI  + "VerifyEmail");

        soapMessage.saveChanges();

        /* Print the request message */
        System.out.print("Request SOAP Message = ");
        soapMessage.writeTo(System.out);
        System.out.println();

        return soapMessage;
    }

    /**
     * Method used to print the SOAP Response
     */
    private static void printSOAPResponse(SOAPMessage soapResponse) throws Exception {
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        Source sourceContent = soapResponse.getSOAPPart().getContent();
        System.out.print("\nResponse SOAP Message = ");
        StreamResult result = new StreamResult(System.out);
        transformer.transform(sourceContent, result);
    }

}

(The code above was taken and adapted from this page.)

(上面的代码取自并改编自此页面。)

回答by justkt

You can call SOAP methods from a variety of programming languages. If you need to do it within Java, you will want to look into JAX-WS. Hereis a tutorial for using JAX-WS with Oracle Forms Builder. To use it you will need Forms Builder 11g.

您可以从各种编程语言调用 SOAP 方法。如果您需要在 Java 中执行此操作,则需要查看JAX-WS是将 JAX-WS 与 Oracle Forms Builder 结合使用的教程。要使用它,您需要 Forms Builder 11g。

You provide the wizard with the URL of the WSDL (web services description language) file. It walks you through the Java code you need to send a message to the SOAP service, deployment, and code import.

您向向导提供 WSDL(Web 服务描述语言)文件的 URL。它会引导您完成将消息发送到 SOAP 服务、部署和代码导入所需的 Java 代码。