如何从Java中的SOAP端点获取响应? [英] How to get response from SOAP endpoint in java?
问题描述
我对SOAP非常陌生,因此一直在网上寻找一些程序,这是我想出的,但是我得到的响应为空,必定是一些愚蠢的事情,但几乎不需要帮助
I am very new to SOAP, so was looking into some programs online, this is what I came up with but I get a null response, must be some silly thing, but need little help
请在下面查看我的代码和输出.谢谢
Please take a look at my code and output below. Thanks
代码
import java.net.URL;
import javax.xml.namespace.QName;
import javax.xml.soap.MessageFactory;
import javax.xml.soap.SOAPBody;
import javax.xml.soap.SOAPBodyElement;
import javax.xml.soap.SOAPConnection;
import javax.xml.soap.SOAPConnectionFactory;
import javax.xml.soap.SOAPElement;
import javax.xml.soap.SOAPHeader;
import javax.xml.soap.SOAPHeaderElement;
import javax.xml.soap.SOAPMessage;
public class AtomicNumber {
public static void main(String[] args) {
try {
SOAPConnectionFactory sfc = SOAPConnectionFactory.newInstance();
SOAPConnection connection = sfc.createConnection();
MessageFactory mf = MessageFactory.newInstance();
SOAPMessage smsg = mf.createMessage();
SOAPHeader shead = smsg.getSOAPHeader();
SOAPBody sbody = smsg.getSOAPBody();
shead.detachNode();
QName bodyName = new QName("http://www.webserviceX.NET", "GetAtomicNumber", "web");
SOAPBodyElement bodyElement = sbody.addBodyElement(bodyName);
QName qn = new QName("ElementName");
SOAPElement quotation = bodyElement.addChildElement(qn);
quotation.addTextNode("iron");
System.out.println("\n Soap Request:\n");
smsg.writeTo(System.out);
System.out.println();
URL endpoint = new URL("http://www.webservicex.net/periodictable.asmx");
SOAPMessage response = connection.call(smsg, endpoint);
System.out.println("\n Soap Response:\n");
System.out.println(response.getContentDescription());
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
我的输出
Soap Request:
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"><SOAP-ENV:Body><web:GetAtomicNumber xmlns:web="http://www.webserviceX.NET"><ElementName>sodium</ElementName></web:GetAtomicNumber></SOAP-ENV:Body></SOAP-ENV:Envelope>
Soap Response:
null
更新
这就是我要得到的(异常)
This is what I am getting (Exception)
<faultcode>soap:Server</faultcode>
<faultstring>System.Web.Services.Protocols.SoapException: Server was unable to process request. ---> System.Data.SqlClient.SqlException: Procedure or function 'GetAtomicNumber' expects parameter '@ElementName', which was not supplied.
at WebServicex.periodictable.GetAtomicNumber(String ElementName)
--- End of inner exception stack trace ---</faultstring>
推荐答案
您想要做的就是为此Web服务自动生成Java代码.WSDL在这里: http://www.webservicex.net/periodictable.asmx?wsdl
What you want to do is auto-generate Java code for this web service. The WSDL is here: http://www.webservicex.net/periodictable.asmx?wsdl
在Java中,用于自动生成代码的工具是 wsimport
.您将要使用以下内容:
In Java, the tool to auto-generate the code is wsimport
. You'll want to use something like this:
wsimport http://www.webservicex.net/periodictable.asmx?wsdl -p com.company.whateveruwant -Xnocompile -d . -keep
这会将所需的代码放入指定的程序包(此处为 com.company.whateveruwant
).
This will put the code you want in the specified package (here com.company.whateveruwant
).
从那里开始,您只需要像普通的Java库一样简单地调用SOAP方法即可:
From there, all you have to do is simply invoke the SOAP method like a normal Java library:
PeriodictableSoap soap = new Periodictable().getPeriodictableSoap();
System.out.println(soap.getAtomicNumber("Iron"));
打印输出:
<NewDataSet>
<Table>
<AtomicNumber>26</AtomicNumber>
<ElementName>Iron</ElementName>
<Symbol>Fe</Symbol>
<AtomicWeight>55.847</AtomicWeight>
<BoilingPoint>3300</BoilingPoint>
<IonisationPotential>7.9</IonisationPotential>
<EletroNegativity>1.6400000000000001</EletroNegativity>
<AtomicRadius>1.17</AtomicRadius>
<MeltingPoint>1808</MeltingPoint>
<Density>7874</Density>
</Table>
</NewDataSet>
这篇关于如何从Java中的SOAP端点获取响应?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!