我很新SOAP WebService开发Java我有以下问题。如何在Java中为此WS创建SOAP请求信封?
我有一个公开2种方法Web服务,第一个(即简单)已经实现(通过其他人),并命名为getVersion(),并且在了SoapUI有以下要求:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tem="http://tempuri.org/">
<soapenv:Header/>
<soapenv:Body>
<tem:getVersion/>
</soapenv:Body>
</soapenv:Envelope>
然后我有exeute在调用Web服务,并在I类有以前的web服务方法跟随着方法的Java项目:
公共字符串getVersion(){ java.net.URL中的URL = NULL ; java.net.URLConnection conn = null;
java.io.BufferedReader rd = null;
String soapResponse = "";
String risultato = "";
// SOAP ENVELOP for the request:
String soapRequest;
soapRequest = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:tem=\"http://tempuri.org/\">" + "<soapenv:Header/>" + "<soapenv:Body> " + "<tem:getVersion/>" + "</soapenv:Body>" + "</soapenv:Envelope>";
try {
// Try to open a connection
url = new java.net.URL(_webServiceUrl);
conn = url.openConnection();
// Set the necessary header fields
conn.setRequestProperty("SOAPAction", "http://tempuri.org/IMyService/getVersion");
conn.setRequestProperty("Content-Type", "text/xml; charset=utf-8");
conn.setDoOutput(true);
// Send the request:
java.io.OutputStreamWriter wr = new java.io.OutputStreamWriter(conn.getOutputStream());
wr.write(soapRequest);
wr.flush();
// Read the response
rd = new java.io.BufferedReader(new java.io.InputStreamReader(conn.getInputStream()));
// Put the entire response into the soapResponse variable:
String line;
while ((line = rd.readLine()) != null) {
//System.out.println(line);
soapResponse = soapResponse + line + System.getProperty("line.separator");
}
rd.close();
// elaboro la risposta
SAXBuilder builder = new SAXBuilder();
org.jdom.Document documentXML = null;
documentXML = builder.build(new StringReader(soapResponse));
XPath xPath;
Element objectElement;
//xPath = XPath.newInstance("s:Envelope/s:Body/getVersionResponse/getVersionResult");
xPath = XPath.newInstance("s:Envelope/s:Body");
xPath.addNamespace("s", "http://schemas.xmlsoap.org/soap/envelope/");
objectElement = (Element) xPath.selectSingleNode(documentXML);
if (objectElement != null) {
risultato = objectElement.getValue();
}
} catch (Exception ex) {
ex.printStackTrace();
}
return risultato;
}
现在我必须复制创建一个新的方法(在调用WS方法的Java项目)的第二种方法同样的事情(这是比较复杂的,因为,不像以前的一个,需要传递一些参数),我对此有些怀疑。
因此,情况是以下之一:在了SoapUI有一个名为getConfigSettings这个WS方法和要求是:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tem="http://tempuri.org/">
<soapenv:Header/>
<soapenv:Body>
<tem:getConfigSettings>
<!--Optional:-->
<tem:login>?</tem:login>
<!--Optional:-->
<tem:password>?</tem:password>
<!--Optional:-->
<tem:ipAddress>?</tem:ipAddress>
<!--Optional:-->
<tem:clientVersion>?</tem:clientVersion>
<!--Optional:-->
<tem:lastUpdateTime>?</tem:lastUpdateTime>
</tem:getConfigSettings>
</soapenv:Body>
</soapenv:Envelope>
As you can see it requires some parameters (in SoapUi I have to replace characters with the correct parameter value)
所以,在Java项目中,我创建了下面的方法执行这个呼吁我的要求创建SOAP信封(但我有关于它的正确性许多doubtsa)
public String authentication(String login, String password, String ipAddress, String clientVersion) {
java.net.URL url = null;
java.net.URLConnection conn = null;
java.io.BufferedReader rd = null;
String myResponse = "";
String soapXml;
// SOAP ENVELOP for the request:
//soapXml = "<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\"> " + "<s:Header>" + "<Action s:mustUnderstand=\"1\" xmlns=\"http://schemas.microsoft.com/ws/2005/05/addressing/none\">http://tempuri.org/IMyService/getVersion</Action>" + "</s:Header>" + "<s:Body>" + "<getVersion xmlns=\"http://tempuri.org/\" />" + "</s:Body>" + "</s:Envelope>";
soapXml = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:tem=\"http://tempuri.org/\">"
+ "<soapenv:Header/>"
+ "<soapenv:Body> "
+ "<tem:login>" + login + "</tem:login>"
+ "<tem:password>" + password + "</tem:password>"
+ "<tem:ipAddress>" + ipAddress + "</tem:ipAddress>"
+ "</soapenv:Body>"
+ "</soapenv:Envelope>";
........................................................
........................................................
........................................................
DO SOME OTHER STUFF
........................................................
........................................................
........................................................
}
正如你可以看到我已经创造了这个SOAP在我所插入的参数外型对我的要求recived作为方法的输入参数:
soapXml = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:tem=\"http://tempuri.org/\">"
+ "<soapenv:Header/>"
+ "<soapenv:Body> "
+ "<tem:login>" + login + "</tem:login>"
+ "<tem:password>" + password + "</tem:password>"
+ "<tem:ipAddress>" + ipAddress + "</tem:ipAddress>"
+ "</soapenv:Body>"
+ "</soapenv:Envelope>";
它是正确的还是我采取了错误的解决办法?一些建议?
TNX
安德烈