以下是.NET Web服务的通用样本SOAP请求,我应该从我的Java web应用程序调用:带有javax.xml.soap的SOAP消息 - 命名空间错误?
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<setAMRequestData xmlns="http://tempuri.org/">
<id>int</id>
</setAMRequestData>
</soap:Body>
</soap:Envelope>
我能够使用这个代码段产生从Java控制台应用程序类似的东西:
import javax.xml.XMLConstants;
import javax.xml.namespace.QName;
import javax.xml.soap.MessageFactory;
import javax.xml.soap.SOAPBody;
import javax.xml.soap.SOAPBodyElement;
import javax.xml.soap.SOAPConnection;
import javax.xml.soap.SOAPConnectionFactory;
import javax.xml.soap.SOAPElement;
import javax.xml.soap.SOAPHeader;
import javax.xml.soap.SOAPMessage;
...
SOAPConnectionFactory sfc = SOAPConnectionFactory.newInstance();
SOAPConnection connection = sfc.createConnection();
MessageFactory mf = MessageFactory.newInstance();
SOAPMessage sm = mf.createMessage();
SOAPHeader sh = sm.getSOAPHeader();
SOAPBody sb = sm.getSOAPBody();
sh.detachNode();
QName bodyName = new QName("http://tempuri.org/", "setAMRequestData", XMLConstants.DEFAULT_NS_PREFIX);
SOAPBodyElement bodyElement = sb.addBodyElement(bodyName);
QName n = new QName("id");
SOAPElement quotation = bodyElement.addChildElement(n);
quotation.addTextNode("121152");
结果是下面的XML:
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP-ENV:Body>
<setAMRequestData xmlns="http://tempuri.org/">
<id>121152</id>
</setAMRequestData>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
这调用服务。接下来,我用的soapUI尝试调用这个服务,它生成的SOAP消息从WSDL这样的(它不同于在信封中的命名空间声明以前,和前缀):
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:tem="http://tempuri.org/">
<soapenv:Header/>
<soapenv:Body>
<tem:setAMRequestData>
<tem:id>?</tem:id>
</tem:setAMRequestData>
</soapenv:Body>
</soapenv:Envelope>
这也来自soapUI。但最后,当我试图使用此代码序列重新创建这种形式的SOAP消息的:
// factories and stuff, like in the example above
SOAPPart part = sm.getSOAPPart();
SOAPEnvelope envelope = part.getEnvelope();
envelope.addNamespaceDeclaration("tem", "http://tempuri.org/");
SOAPHeader sh = sm.getSOAPHeader();
SOAPBody sb = sm.getSOAPBody();
sh.detachNode();
QName bodyName = new QName(null, "setAMRequestData", "tem");
SOAPBodyElement bodyElement = sb.addBodyElement(bodyName);
QName n = new QName(null, "id", "tem");
SOAPElement quotation = bodyElement.addChildElement(n);
quotation.addTextNode("7028");
我在线路的SOAPElement报价以下异常= bodyElement.addChildElement(N);:
org.w3c.dom.DOMException中:NAMESPACE_ERR:试图创建或在某种程度上这是不正确关于命名空间改变的对象。
不管我试过了,我根本无法设置“TEM”前缀为ID元素......这到底是怎么回事?
谢谢。
是的,这解决了它。显然我遇到了javax.xml.soap用法的一些不好的例子...谢谢。 – Less