简体   繁体   English

调用SOAP的Java类-Web服务

[英]Java class to call a SOAP - Web service

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 . 我有一个需要从Oracle调用的SOAP,并且我听说解决该问题的唯一方法是通过Java类。不幸的是,由于我是Oracle开发人员(Oracle Forms),所以我对Java不熟悉,对此我非常感谢如果有人可以帮助我创建一个调用此SOAP的类,以便可以在我的Oracle数据库上构建它,并以调用函数的方式从Oracle Forms Builder对其进行调用。

There are two SOAPs (1.1 nd 1.2 ) , both of any can work : 有两种SOAP(1.1和1.2),两者都可以工作:

* SOAP 1.1 * SOAP 1.1

The following is a sample SOAP 1.1 request and response. 以下是示例SOAP 1.1请求和响应。 The placeholders shown need to be replaced with actual values. 显示的占位符需要替换为实际值。

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 * * SOAP 1.2

The following is a sample SOAP 1.2 request and response. 以下是SOAP 1.2请求和响应的示例。 The placeholders shown need to be replaced with actual values. 显示的占位符需要替换为实际值。

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>

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. 带有Java附件API的SOAP(SAAJ)主要用于直接处理任何Web Service API幕后发生的SOAP请求/响应消息。 It allows the developers to directly send and receive soap messages instead of using JAX-WS. 它允许开发人员直接发送和接收肥皂消息,而不是使用JAX-WS。

See below a working example (run it!) of a SOAP web service call using SAAJ. 参见下面使用SAAJ进行SOAP Web服务调用的工作示例(运行它!)。 It calls this web service . 它称为此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>mutantninja@gmail.com</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("mutantninja@gmail.com");
        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 .) (以上代码已从此页面获取并改编。)

You can call SOAP methods from a variety of programming languages. 您可以从多种编程语言中调用SOAP方法。 If you need to do it within Java, you will want to look into JAX-WS . 如果您需要在Java中执行此操作,则需要研究JAX-WS Here is a tutorial for using JAX-WS with Oracle Forms Builder. 是将JAX-WS与Oracle Forms Builder结合使用的教程。 To use it you will need Forms Builder 11g. 要使用它,您将需要Forms Builder 11g。

You provide the wizard with the URL of the WSDL (web services description language) file. 您为向导提供了WSDL(Web服务描述语言)文件的URL。 It walks you through the Java code you need to send a message to the SOAP service, deployment, and code import. 它引导您完成向SOAP服务,部署和代码导入发送消息所需的Java代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM