带有 javax.xml.soap 的 SOAP 消息 - 命名空间错误? [英] SOAP message with javax.xml.soap - namespace error?

查看:69
本文介绍了带有 javax.xml.soap 的 SOAP 消息 - 命名空间错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是我应该从我的 Java Web 应用程序调用的 .NET Web 服务的通用示例 SOAP 请求:

The following is a generic sample SOAP request for .NET web service I'm supposed to invoke from my java web app:

<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 控制台应用程序生成类似的内容:

I am able to generate something similar from java console application using this code segment:

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 来尝试调用这个服务,它从 WSDL 生成soap 消息是这样的(它与前面的在信封中的命名空间声明中不同,以及前缀):

This invokes the service. Next, I used soapUI to try to invoke this service, and it generated soap message from WSDL like this (it differs from the previous in the namespace declaration in envelope, and prefixes):

<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.但最后,当我尝试使用以下代码序列重新创建这种形式的肥皂消息时:

This also works from soapUI. But finally, when I tried to recreate this form of soap message using this code sequence:

// 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 quote = bodyElement.addChildElement(n); 行中遇到以下异常:

I got the following exception in line SOAPElement quotation = bodyElement.addChildElement(n);:

org.w3c.dom.DOMException: NAMESPACE_ERR: 尝试以不正确的命名空间方式创建或更改对象.

org.w3c.dom.DOMException: NAMESPACE_ERR: An attempt is made to create or change an object in a way which is incorrect with regard to namespaces.

无论我尝试什么,我都无法为 id 元素设置tem"前缀...这里发生了什么?

No matter what I tried, I simply couldn't set "tem" prefix for the id element... What is going on here?

谢谢.

推荐答案

您正在将命名空间 uri 绑定到前缀,然后尝试创建具有相同前缀但命名空间 uri 为空的元素:

You are binding a namespace uri to a prefix and later try to create an element with the same prefix but with a null namespace uri:

envelope.addNamespaceDeclaration("tem", "http://tempuri.org/");
...
QName bodyName = new QName(null, "setAMRequestData", "tem");

元素由命名空间 uri 和本地名称的组合标识.为了解决这个问题,你必须在你创建的每个元素上指定命名空间:

An element is identified by the combination of namespace uri and local name. To solve this problem you have to specify the namespace on each element you create:

QName bodyName = new QName("http://tempuri.org/", "setAMRequestData", "tem");
...
QName n = new QName("http://tempuri.org/", "id", "tem");

这篇关于带有 javax.xml.soap 的 SOAP 消息 - 命名空间错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆