2

我对 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:

<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

更新

这就是我得到的(例外)

<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>
4

5 回答 5

4

您要做的是为此 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"));

这打印出来:

<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>
于 2012-09-27T12:49:36.053 回答
1

尝试

   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");
于 2012-09-27T12:35:05.387 回答
0

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

<dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-frontend-jaxws</artifactId>
            <version>2.4.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-transports-http</artifactId>
            <version>2.4.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-core</artifactId>
            <version>2.4.2</version>
        </dependency>

并添加此构建部分:

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.生成。班级

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

希望能帮助到你!

于 2012-09-27T13:09:38.090 回答
0

@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();
于 2013-02-25T05:48:19.657 回答
0

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

更多信息(包括 ANT)在这里

于 2012-09-27T12:08:31.207 回答