0

我正在尝试将 SOAP 调用集成到我的 Java 应用程序中。我已经在线学习了各种教程,但是当我运行应用程序时,它会给出错误,sm.saveChanges()如果该行被注释掉,它会给出错误sm.writeTo(System.out)

下面是代码:

//Create a SOAPConnection   
          SOAPConnectionFactory sfc = SOAPConnectionFactory.newInstance();
          SOAPConnection connection = sfc.createConnection();

          MessageFactory mf = MessageFactory.newInstance();
          SOAPMessage sm = mf.createMessage();
          SOAPPart sp = sm.getSOAPPart();
          SOAPEnvelope sv = sp.getEnvelope();

          SOAPHeader sh = sm.getSOAPHeader();
          //SOAPHeaderElement headerElement = sh.addHeaderElement(sv.createName("Signature", "SOAP-SEC", "http://schemas.xmlsoap.org/soap/security/2000-12"));

          SOAPBody sb = sm.getSOAPBody();

          //sh.detachNode();


          MimeHeaders headers = sm.getMimeHeaders();
          headers.addHeader("SOAPAction", "\"\""); 
          //QName bodyName = new QName("http://quoteCompany.com", "GetQuote", "d");
          //QName bodyName = new QName(msisdn, "GetRioInformationsRequest", "ser");
          //QName bodyName = new QName("http://schemas.xmlsoap.org/soap/encoding/", "GetRioInformationsRequest", XMLConstants.DEFAULT_NS_PREFIX);
          QName bodyName = new QName("http://quoteCompany.com/", "GetQuote", "ser");
          //QName bodyName = sv.createQName("http://quoteCompany.com/", "GetQuote", "ser");
          Name  bodyName2 = sv.createName("GetQuote","ser", "http://quoteCompany.com/");
          SOAPBodyElement bodyElement = sb.addBodyElement(bodyName2);
          //QName qn = new QName("aName");
          //Name qn = sv.createName("rioSearchRequest");
          QName qn = new QName("returnCode");

          SOAPElement quotation = bodyElement.addChildElement(qn);
          //quotation.addChildElement(qn2);

          //Name qn2 = sv.createName("msisdn");
          //qn2.
          //quotation.addAttribute(qn2, msisdn);
          //quotation.addTextNode("TextMode");
          quotation.addTextNode(msisdn);

          System.out.println("Saving SOAP message.");
          sm.saveChanges();
          System.out.println("\n Soap Request:\n");
          sm.writeTo(System.out);
          System.out.println("Request Outputted for viewing.");

          //URL endpoint = new URL("http://yourServer.com");
          URL endpoint = new URL("http://127.0.0.1:8088/mockSoapBinding");
          System.out.println("URL entered.");
          SOAPMessage response = connection.call(sm, endpoint);
          System.out.println("Resquest Sent!");
          System.out.println(response.getContentDescription());
          System.out.println("Response obtained!");

相应的错误是:

    XSL-1101: (Fatal Error) DOMSource node as this type not supported.
Jun 27, 2012 4:37:45 PM com.sun.xml.messaging.saaj.soap.MessageImpl saveChanges
SEVERE: SAAJ0539: Unable to get header stream in saveChanges
Jun 27, 2012 4:37:45 PM com.sun.xml.messaging.saaj.soap.MessageImpl saveChanges
SEVERE: SAAJ0540: Error during saving a multipart message
SOAP Call Issue: Error during saving a multipart message
com.sun.xml.messaging.saaj.SOAPExceptionImpl: Error during saving a multipart message
    at com.sun.xml.messaging.saaj.soap.MessageImpl.saveChanges(MessageImpl.java:1209)
    at com.application.package.SOAPCalls.SOAPCall(SOAPCalls.java:66)
    at com.application.package.Main.<init>(Main.java:97)
    at com.application.package.Main.main(Main.java:253)
Caused by: com.sun.xml.messaging.saaj.SOAPExceptionImpl: Unable to get header stream in saveChanges: 
    at com.sun.xml.messaging.saaj.soap.MessageImpl.saveChanges(MessageImpl.java:1179)
    ... 3 more
Caused by: java.io.IOException: XSL-1101: (Fatal Error) DOMSource node as this type not supported.
    at com.sun.xml.messaging.saaj.soap.impl.EnvelopeImpl.output(EnvelopeImpl.java:306)
    at com.sun.xml.messaging.saaj.soap.impl.EnvelopeImpl.output(EnvelopeImpl.java:317)
    at com.sun.xml.messaging.saaj.soap.SOAPPartImpl.getContentAsStream(SOAPPartImpl.java:324)
    at com.sun.xml.messaging.saaj.soap.MessageImpl.getHeaderBytes(MessageImpl.java:1020)
    at com.sun.xml.messaging.saaj.soap.MessageImpl.saveChanges(MessageImpl.java:1171)
    ... 3 more

有什么建议么?

4

3 回答 3

3

您真的不想手动执行此操作。生成一个 Web 服务客户端并使用该客户端。

您可以使用Apache Axis WSDL2Code。还有其他选项可以生成客户端。例如 Eclipse 有一个内置的工具可以做到这一点。

于 2012-06-27T22:08:02.117 回答
0

同意 Udo Held 等人的观点。只想提一下,如果您真的想在如此低的级别上集成 SOAP 调用,您可以首先使用 Apache Axis 来完成,而不是使用调试和 http 数据包跟踪工具进行一些调查以找出您遗漏的内容。例如,当您尝试使用 XPath 从 SOAP 响应中拉出单个节点而不首先将其绑定到 POJO 对象时,这种方式对于某些特定情况来说是值得的。

于 2012-06-28T05:50:26.730 回答
0

正如@Udo Held 所提到的,您不想手动构建SOAP 请求。

请参阅此问题的答案:如何从 SOAP 获得响应?

您可以使用该wsimport工具自动生成连接到 SOAP 服务所需的所有代码。

wsimport随 JDK 一起提供,因此您已经在系统上安装了它(无需额外下载)并假设 Java 在您的路径上,您可以转到任何终端并键入:

wsimport http://www.url.to.wsdl.asmx?wsdl -p com.whateveruwant -Xnocompile -d . -keep

完成此操作后,您将能够非常简单地使用自动生成的代码调用 Web 服务,例如:

CustomInterface soap = new CustomEndpoint().getCustomInterface();
System.out.println(soap.getAnswerFromWs("ParamValue"));
于 2012-09-27T14:01:56.340 回答