我在使用Web服务的客户端实现时遇到了一些麻烦。我有wsdl,我用wsimport工具创建了类。
当我通过这些类调用webservice时,我可以在日志中看到请求和响应...它看起来没问题。 我不能使用这些类来处理来自WS的收到的数据,因为结果对象没有关于它包含的字段的定义......
因此我试图从WebService获取原始响应...我通过附加SoapHandler来做到这一点:
BindingProvider bindingProvider = (BindingProvider)port;
List<Handler> handlerChain = new ArrayList<Handler>();
handlerChain.add(new SoapMsgHandler());
bindingProvider.getBinding().setHandlerChain(handlerChain);
result = port.getXmlFromWS();
....
在处理程序中我实现了handleMessage(SOAPMessageContext context)方法将消息打印到日志中,但是我遇到了问题...
下面是应该处理响应的Handler类的一部分...但是我在这一行得到一个异常:SOAPMessage message = smc.getMessage()
例外情况如下:
org.w3c.dom.DOMException: WRONG_DOCUMENT_ERR: A node is used in a different document than the one that created it.
at org.apache.xerces.dom.ParentNode.internalInsertBefore(Unknown Source)
at org.apache.xerces.dom.ParentNode.insertBefore(Unknown Source)
at org.apache.xerces.dom.CoreDocumentImpl.insertBefore(Unknown Source)
at org.apache.xerces.dom.NodeImpl.appendChild(Unknown Source)
at org.jboss.ws.core.soap.SOAPDocument.appendChild(SOAPDocument.java:222)
at org.jboss.ws.core.soap.SOAPPartImpl.appendChild(SOAPPartImpl.java:297)
at com.sun.xml.bind.marshaller.SAX2DOMEx.startElement(SAX2DOMEx.java:176)
at com.sun.xml.ws.encoding.TagInfoset.writeStart(TagInfoset.java:168)
at com.sun.xml.ws.message.stream.StreamMessage.writeTo(StreamMessage.java:442)
at com.sun.xml.ws.message.AbstractMessageImpl.readAsSOAPMessage(AbstractMessageImpl.java:193)
at com.sun.xml.ws.handler.SOAPMessageContextImpl.getMessage(SOAPMessageContextImpl.java:79)
处理程序实现的一部分:
import javax.xml.soap.SOAPMessage;
import javax.xml.ws.handler.MessageContext;
import javax.xml.ws.handler.soap.SOAPHandler;
import javax.xml.ws.handler.soap.SOAPMessageContext;
public class SoapMsgHandler implements SOAPHandler<SOAPMessageContext> {
@Override
public boolean handleMessage(SOAPMessageContext context) {
printTheMsg(context);
return true;
}
private void printTheMsg(SOAPMessageContext smc) {
SOAPMessage message = smc.getMessage(); //this line produces the mentioned exception
....
}
....
}
对于可能出错的任何帮助都将非常感激。