2010-08-24 139 views
0

嘿,在那裏,我一直在使用JWSDL來讓我以編程方式使用WSDL文件。我現在想創建可以發送到服務器的SOAP請求。如何從JWSDL類生成這些請求?有任何想法嗎?從JWSDL創建SOAP請求

謝謝!

+0

JWSDL文檔建議你做什麼? – 2010-08-24 20:41:00

+0

實際文檔沒有提及擴展的用法。但是,我不明白我的目的是使用擴展名。 – rel1kz 2010-08-24 20:51:41

回答

0

你可以這樣說:

在這裏,我創建了一個簡單的Web服務,它有兩個參數 數字1和數字2。並給出答案爲number3(= number1 + number2)。 Web服務已經部署在localhost:8080(Tomcat的 服務器)

你的答案從這裏開始

我已經創建了一個示例Java檔案...其中兩個參數傳遞給在SOAP請求一個 web服務,並得到從web服務 SOAP響應。您可以從WSDL文件中獲取getCalculation,m,localhost:8080,number1,number2和url等參數(以代碼形式描述)。

示例代碼:

package SampleJavaWSDLDemo; 
  • 進口javax.xml.soap中*;
  • import java.util。*;
  • import java.net.URL;
  • import javax.xml.transform。*;
  • import javax.xml.transform.stream.StreamResult;
  • import javax.xml.soap.SOAPConnectionFactory;
  • import javax.xml.soap.SOAPConnection;
  • import javax.xml.soap.MessageFactory;
  • import javax.xml.soap.SOAPMessage;
  • import javax.xml.soap.SOAPPart;
  • import javax.xml.soap.SOAPEnvelope;
  • import javax.xml.soap.SOAPBody;
  • import java.net.URL;

公共類SampleJavaWSDLDemo {

public static void main(String[] args) 
{ 
    try { 

    //Create a SOAPMessage 
    SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance(); 
    SOAPConnection connection = soapConnectionFactory.createConnection(); 
    SOAPFactory soapFactory = SOAPFactory.newInstance(); 
    MessageFactory factory = MessageFactory.newInstance(); 
    SOAPMessage message = factory.createMessage(); 
    SOAPHeader header = message.getSOAPHeader(); 
    SOAPBody body = message.getSOAPBody(); 
    header.detachNode(); 

    Name bodyName = soapFactory.createName("getCalculation", "m", "http://localhost:8080/"); 
    SOAPBodyElement bodyElement = body.addBodyElement(bodyName); 

      //Insert Content 
    Name name = envelope.createName("number1"); 
    SOAPElement symbol = bodyElement.addChildElement(name); 
    symbol.addTextNode("10"); 
    name = envelope.createName("number2"); 
    symbol = bodyElement.addChildElement(name); 
    symbol.addTextNode("20"); 

      System.out.println("\n Request: \n"); 
      message.writeTo(System.out); 
      System.out.println(); 

      // Create an endpint point which is either URL or String type 
    URL endpoint = new URL("http://localhost:8080/WebServiceName/OperationName"); 

      //Send a SOAPMessage (request) and then wait for SOAPMessage (response) 
    SOAPMessage response = connection.call(message, endpoint); 

    // Get the response from the webservice.     
    SOAPBody soapBody = response.getSOAPBody(); 

    System.out.println("\n Response: \n"); 
    TransformerFactory transformerfactory = TransformerFactory.newInstance(); 
    Transformer transformer = transformerfactory.newTransformer(); 
    Source sourceContent = response.getSOAPPart().getContent(); 
    StreamResult result = new StreamResult(System.out); 
    transformer.transform(sourceContent, result); 
    System.out.println(); 
    String resp = response.getSOAPBody().getElementsByTagName("return").item(0).getFirstChild().getNodeValue(); 
    System.out.println("Answer is: " + resp); 

    connection.close(); 

    } catch (Exception ex) { 
     ex.printStackTrace(); 
    } 
} 

}

嘗試運行這段代碼。 它可以給你一個完整的肥皂請求和響應消息。