我使用 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() 时最终都会出现相同类型的错误。
最佳答案
不知道你有没有找到解决办法。我最近刚刚看到完全相同的错误,这是由于未设置 TransformerFactory 造成的。
我使用了 Saxon 库中的 TransformerFactory - 可以获取其 jar here .
然后我设置一个引用 Saxon TransformerFactory 的系统属性:
System.setProperty("javax.xml.transform.TransformerFactory",
"net.sf.saxon.TransformerFactoryImpl");
当我重新运行代码时,错误消失了。
以上只是设置TransformerFactory的一种方式。我在这里找到了许多其他方法来执行此操作:stackoverflow.com/questions/11314604/
关于Java - 从字符串 XML 信封使用 SOAP Web 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16042993/