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

2024-01-04

我对 SOAP 很陌生,所以在网上查看了一些程序,这是我想出的,但我得到了一个空响应,一定是一些愚蠢的事情,但需要很少的帮助

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

Code

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

Update

这就是我得到的(例外)

<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 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"));

这打印出:

<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>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何从java中的SOAP端点获取响应? 的相关文章

随机推荐