无法使用Java SoapMessage更改信封中的soap URI [英] Cannot change soap URI in envelope using java SoapMessage
问题描述
我正在尝试创建一个简单的SOAP消息以从客户端发送,但是(似乎)无法更改信封中"soap"名称空间的URI.
I'm trying to create a simple SOAP message to send from a client, but I am (seemingly) unable to change the URI of the "soap" namespace in the envelope.
soap header应该是这样的:
This is what the soap header SHOULD look like:
<soap:Envelope xmlns:soap="http://www.w3.org/2001/12/soap-envelope/" soap:encodingStyle="http://www.w3.org/2001/12/soap-encoding/"> ... </soap:Envelope>
所以我有以下代码:
final SOAPMessage sm = MessageFactory.newInstance().createMessage();
final SOAPPart sp = sm.getSOAPPart();
final SOAPEnvelope se = sp.getEnvelope();
final SOAPHeader sh = se.getHeader();
final SOAPBody sb = se.getBody();
se.removeNamespaceDeclaration(se.getPrefix());
se.addNamespaceDeclaration("soap", "http://www.w3.org/2001/12/soap-envelope");
se.setPrefix("soap");
sb.setPrefix("soap");
sh.setPrefix("soap");
se.setEncodingStyle("http://www.w3.org/2001/12/soap-encoding/");
但是,当我在发送前打印邮件时,以下是我的信封:
However, when I print the message before sending, the following is my envelope:
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" soap:encodingStyle="http://www.w3.org/2001/12/soap-encoding/">
请注意应该"部分中xmlns:soap的URI与实际URI中的区别.
Notice the differences in the URIs of xmlns:soap in the "should-be" section and the actual.
如果我将 addNamespaceDeclaration
调用的第一个参数更改为"soapy"而不是"soap",则得到以下信封:
If I change the first argument of the addNamespaceDeclaration
call to "soapy" instead of "soap", this is the following envelope I get:
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:soapy="http://www.w3.org/2001/12/soap-envelope" soap:encodingStyle="http://www.w3.org/2001/12/soap-encoding/">
我猜这可能与调用为 addNamespaceDeclaration
而不是诸如 changeNamespaceDeclaration
之类的事实有关,并且考虑到命名空间已被忽略目前,但是我找不到有效的方法(我已经尝试过 setAttributeNS
).
I'm guessing it may have something to do with the fact that the call is addNamespaceDeclaration
rather than something like changeNamespaceDeclaration
, and it is ignored considering the namespace is already present, but I cannot find something that works (I've already tried setAttributeNS
).
我刚刚意识到 setAttributeNS
是愚蠢的,因为这改变了名称空间,而不是URI.再次我有点困惑,当我继续搜索时,有时会看到命名是 soap:"Namespace"
,所以从这个意义上讲,我确实想更改命名空间...但是我以为命名空间是肥皂"的一部分.有任何澄清吗?
I just realized that setAttributeNS
is silly because that's changing the namespace, not the URI.
EDIT AGAIN: I'm a little confused, as I continue to search I see sometimes that the naming goes soap:"Namespace"
, so in that sense I do want to change the namespace... but I thought the namespace was the "soap" part. Any clarifications?
这是我的第一篇文章,因此,如果我要问的是已经解决的问题,我深表歉意,但是我四处搜寻,发现的大部分内容都与更改名称空间有关(例如,从SOAP-ENV,这是默认的名称空间,而不是URI本身.预先感谢.
This is my first post so I apologize if I'm asking something that has already been solved, but I've searched around and most of what I've found is related to changing the namespace (like from SOAP-ENV, which is the default namespace, to soap) rather than the URI itself. Thanks in advance.
-M
推荐答案
通常,您不需要手动修改SOAP名称空间.您可能想要实现的是创建一个SOAP 1.2消息(它的名称空间与SOAP 1.1不同).尝试从代码中删除所有更改名称空间的行,并将第一行更改为
In general you should not need to manually modify the SOAP namespace. What you probably want to achieve is to create a SOAP 1.2 message (which has a different namespace than SOAP 1.1). Try removing all namespace altering lines from your code and change the first line to
final SOAPMessage sm = MessageFactory.newInstance(SOAPConstants.SOAP_1_2_PROTOCOL).createMessage();
如果您确实需要指定应使用的前缀,则此代码似乎有效:
In case you REALLY need to specify which prefix that should be used, this code seems to work:
SOAPMessage sm = MessageFactory.newInstance(SOAPConstants.SOAP_1_2_PROTOCOL).createMessage();
sm.getSOAPPart().getEnvelope().setPrefix("soap");
sm.getSOAPPart().getEnvelope().removeNamespaceDeclaration("env");
sm.getSOAPHeader().setPrefix("soap");
sm.getSOAPBody().setPrefix("soap");
这篇关于无法使用Java SoapMessage更改信封中的soap URI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!