简体   繁体   English

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

[英]How to get response from SOAP endpoint in java?

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 我是SOAP的新手,所以一直在网上寻找一些程序,这是我想出的,但是我得到的响应为空,一定是一些愚蠢的事情,但几乎不需要帮助

Please take a look at my code and output below. 请在下面查看我的代码和输出。 Thanks 谢谢

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();
    }
}
}

My Output 我的输出

 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 更新

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>

What you want to do is auto-generate Java code for this web service. 您要做的是为此Web服务自动生成Java代码。 The WSDL is here: http://www.webservicex.net/periodictable.asmx?wsdl WSDL在这里: http : //www.webservicex.net/periodictable.asmx?wsdl

In Java, the tool to auto-generate the code is wsimport . 在Java中,用于自动生成代码的工具是wsimport You'll want to use something like this: 您将要使用以下内容:

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

This will put the code you want in the specified package (here com.company.whateveruwant ). 这会将所需的代码放入指定的程序包(在此处com.company.whateveruwant )。

From there, all you have to do is simply invoke the SOAP method like a normal Java library: 从那里开始,您只需要像普通的Java库一样简单地调用SOAP方法即可:

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

This prints out: 打印输出:

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

Try with 试试看

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

EDIT: Also, as others have suggested - you will be better off using generated client code here - Axis, JAX-WS etc are all options. 编辑:此外,正如其他人所建议的-您将在这里使用生成的客户端代码更好-Axis,JAX-WS等都是选择。

The correct code should be as below. 正确的代码应如下所示。

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

Do you really need to use bare SOAP classes? 您是否真的需要使用裸露的SOAP类? How about generating JAX-WS artifacts to get rid of all this boilerplate? 如何生成JAX-WS工件以摆脱所有这些样板?

More info (incorporating ANT) here . 在此处了解更多信息(并入ANT)。

With cxf-codegen-plugin maven plugin you can create a client of the soap service quickly by adding these dependencies: 使用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>

and adding this build section: 并添加以下构建部分:

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

then inside ${basedir}/target/generated-sources/cxf you will have classes required to call the web service and an example of how to do it. 然后在$ {basedir} / target / generated-sources / cxf中,您将具有调用Web服务所需的类以及如何执行此操作的示例。

Hope it helps! 希望能帮助到你!

@Ricky, this is the correct code. @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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM