2010-08-24 132 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(); 
    } 
} 

}

尝试运行这段代码。 它可以给你一个完整的肥皂请求和响应消息。