从JWSDL创建SOAP请求 [英] Create SOAP Request from JWSDL
问题描述
嘿,我一直在使用JWSDL,以使我能够以编程方式使用WSDL文件.现在,我想创建可以发送到服务器的SOAP请求.如何从JWSDL类生成这些请求?有任何想法吗?
Hey there, I have been working with JWSDL to allow me to programatically work with WSDL files. I now want to create SOAP requests that can be sent to the server. How do I generate these requests from the JWSDL classes? any ideas?
谢谢!
推荐答案
您可以这样做:
在这里,我创建了一个样本Web服务,该服务包含两个参数 数字1和数字2.并将响应表示为number3(= number1 + 2号). Web服务已部署在localhost:8080(tomcat 服务器)
Here, i have created a sample web service which takes two parameters number1 and number2. And gives the response as number3 (= number1 + number2). Web service is already deployed on localhost:8080 (tomcat server)
您的答案从这里开始.
我已经创建了一个示例Java文件...该文件将两个参数传递给了 SOAP请求中的Web服务,并从Web获取SOAP响应 服务.您可以从WSDL文件中获取参数(在代码中描述),例如getCalculation,m,localhost:8080,number1,number2和url.
i have created a sample java file... which passes two parameters to a web service in a SOAP request and gets the SOAP response from the web service. You can get the parameters(described in a code) like getCalculation ,m , localhost:8080, number1, number2 and url from the WSDL file.
示例代码:
package SampleJavaWSDLDemo;
- 导入javax.xml.soap.*;
- 导入java.util.*;
- 导入java.net.URL;
- 导入javax.xml.transform.*;
- 导入javax.xml.transform.stream.StreamResult;
- 导入javax.xml.soap.SOAPConnectionFactory;
- 导入javax.xml.soap.SOAPConnection;
- 导入javax.xml.soap.MessageFactory;
- 导入javax.xml.soap.SOAPMessage;
- 导入javax.xml.soap.SOAPPart;
- 导入javax.xml.soap.SOAPEnvelope;
- 导入javax.xml.soap.SOAPBody;
- 导入java.net.URL;
- import 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 class 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();
}
}
}
尝试运行此代码. 它可以为您提供完整的肥皂请求和响应消息.
Try to run this code. It can give you a whole soap request and response message.
这篇关于从JWSDL创建SOAP请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!