如何从java中获取SOAP端点的响应?

我是SOAP的新手,所以在线查看一些程序,这是我想出来的但我得到的回复是空的,必须是一些愚蠢的事情,但需要一些帮助

请看下面的代码和输出。 谢谢

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: sodium Soap Response: null 

更新

这就是我得到的(例外)

 soap:Server 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 --- 

您要做的是为此Web服务自动生成Java代码。 WSDL在这里: http : //www.webservicex.net/periodictable.asmx?wsdl

在Java中,自动生成代码的工具是wsimport 。 你会想要使用这样的东西:

 wsimport http://www.webservicex.net/periodictable.asmx?wsdl -p com.company.whateveruwant -Xnocompile -d . -keep 

这会将您想要的代码放在指定的包中(此处为com.company.whateveruwant )。

从那里,您所要做的就是像普通的Java库一样调用SOAP方法:

 PeriodictableSoap soap = new Periodictable().getPeriodictableSoap(); System.out.println(soap.getAtomicNumber("Iron")); 

打印出:

  26IronFe55.84733007.91.64000000000000011.1718087874

试试吧

  QName qn = new QName("http://www.webserviceX.NET","ElementName","web"); 

编辑:此外,正如其他人所建议的那样 – 你最好在这里使用生成的客户端代码 – Axis,JAX-WS等都是选择。

正确的代码应如下所示。

  QName bodyName = new QName("http://www.webserviceX.NET", "GetAtomicNumber"); SOAPBodyElement bodyElement = sbody.addBodyElement(bodyName); QName qn = new QName("ElementName"); 

你真的需要使用裸SOAP类吗? 如何生成JAX-WS工件以摆脱所有这些样板?

更多信息(包含ANT) 在这里 。

使用cxf-codegen-plugin maven插件,您可以通过添加这些依赖项来快速创建soap服务的客户端:

  org.apache.cxf cxf-rt-frontend-jaxws 2.4.2   org.apache.cxf cxf-rt-transports-http 2.4.2   org.apache.cxf cxf-rt-core 2.4.2  

并添加此构建部分:

org.apache.cxf cxf-codegen-plugin 2.1.2 generate-sources generate-sources wsdl2java $ {basedir} / target / generated-sources / cxf $ {basedir} /src/main/resources/wsdlfile.wsdl -client -wsdlLocation -p com.package.for.generated.classes

然后在$ {basedir} / target / generated-sources / cxf中,您将拥有调用Web服务所需的类以及如何执行此操作的示例。

希望能帮助到你!

@Ricky,这是正确的代码。

 SOAPBody body = message.getSOAPBody(); QName bodyName = new QName("http://www.webserviceX.NET", "GetAtomicNumber"); SOAPBodyElement bodyElement = body.addBodyElement(bodyName); SOAPElement symbol = bodyElement.addChildElement("MyMetal"); symbol.addTextNode("iron"); SOAPConnection connection = SOAPConnectionFactory.newInstance().createConnection(); SOAPMessage response = connection.call(message, endpoint); connection.close(); message.writeTo(System.out); System.out.println(); response.writeTo(System.out); System.out.println();