【问题标题】:Java - Consume SOAP Web Service from String XML EnvelopeJava - 从字符串 XML 信封中使用 SOAP Web 服务
【发布时间】:2013-04-16 17:06:53
【问题描述】:

使用 SoapUI,我构建了以下 XML 信封并将其封装在一个字符串中。

ProcessJournal 是一个没有参数的方法,返回一个字符串。

String soapText = "<Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:upd=\"http://webservices.vm.vmc/UpdateCMA/\">" +
                    "<Header/>" +
                     "<Body>" +
                       "<upd:ProcessJournal/>" +
                     "</Body>" +
                  "</Envelope>";

现在...我只想调用上面定义的 Web 服务。并找到了一些示例代码

                // Create SoapMessage
                MessageFactory msgFactory     = MessageFactory.newInstance();
                SOAPMessage message           = msgFactory.createMessage();
                SOAPPart soapPart             = message.getSOAPPart();

                // Load the SOAP text into a stream source
                byte[] buffer                 = soapText.getBytes();
                ByteArrayInputStream stream   = new ByteArrayInputStream(buffer);
                StreamSource source           = new StreamSource(stream);

                // Set contents of message 
                soapPart.setContent(source);

                // -- DONE
                message.writeTo(System.out);


                //Try accessing the SOAPBody
                SOAPBody soapBody = message.getSOAPBody();

问题是,在 message.getSOAPBody();,我得到一个错误

XML-22103: (Fatal Error) DOMResult can not be this kind of node.
Apr 16, 2013 12:05:06 PM com.sun.xml.internal.messaging.saaj.soap.EnvelopeFactory createEnvelope
SEVERE: SAAJ0511: Unable to create envelope from given source

我找到的所有各种示例,在执行 getSOAPBody() 时都会出现相同类型的错误。

【问题讨论】:

    标签: java xml web-services soap envelope


    【解决方案1】:

    我不知道您是否找到了解决方案。我最近刚刚看到完全相同的错误,这是由于未设置 TransformerFactory。

    我使用了来自 Saxon 库的 TransformerFactory - 可以从 here 获得它的 jar。

    然后我设置一个引用 Saxon TransformerFactory 的系统属性:

    System.setProperty("javax.xml.transform.TransformerFactory",    
            "net.sf.saxon.TransformerFactoryImpl");
    

    当我重新运行我的代码时,错误就消失了。

    以上只是设置 TransformerFactory 的一种方法。我在这里找到了许多其他方法可以做到这一点:stackoverflow.com/questions/11314604/

    【讨论】:

    • 感谢您的帖子。我们最终走了一条不同的路线,因为没有人可以让它发挥作用。但是,当需要重试时,我会尝试您的建议 :) 谢谢!
    【解决方案2】:

    看起来问题出在您的 soapText 中的命名空间前缀。只需按如下方式修改您的soapText,我就能够毫无错误地执行您的示例,并且无需手动设置任何TransformerFactory:

    String soapText = 
         "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:upd=\"http://webservices.vm.vmc/UpdateCMA/\">" +
            "<soapenv:Header/>" +
            "<soapenv:Body>" +
              "<upd:ProcessJournal/>" +
            "</soapenv:Body>" +
         "</soapenv:Envelope>";
    

    注意在所有 Soap 元素中添加的 soapenv: 前缀与您的 Soap 服务命名空间中的 upd 前缀形成对比。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-02-22
      • 2015-11-20
      • 2014-03-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多