使用SoapUI,我构建了以下XML信封并将其封装在String中。

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()时都会出现相同类型的错误。

最佳答案

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

我使用了Saxon库中的TransformerFactory-可以通过here获得的jar。

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

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


重新运行代码后,错误消失了。

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

09-20 17:19