无法使用 java SoapMessage 更改信封中的soap URI

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10286209/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 00:22:48  来源:igfitidea点击:

Cannot change soap URI in envelope using java SoapMessage

javauri

提问by MandM

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 消息以从客户端发送,但我(似乎)无法更改信封中“soap”命名空间的 URI。

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>

So I have the following code:

所以我有以下代码:

    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/">

Notice the differences in the URIs of xmlns:soap in the "should-be" section and the actual.

请注意“应该”部分中 xmlns:soap 的 URI 与实际的不同。

If I change the first argument of the addNamespaceDeclarationcall to "soapy" instead of "soap", this is the following envelope I get:

如果我将addNamespaceDeclaration调用的第一个参数更改为“soapy”而不是“soap”,我得到以下信封:

<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/">

I'm guessing it may have something to do with the fact that the call is addNamespaceDeclarationrather 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).

我猜这可能与调用是addNamespaceDeclaration而不是类似的事实有关,changeNamespaceDeclaration考虑到命名空间已经存在,它被忽略,但我找不到有用的东西(我已经尝试过setAttributeNS)。

EDIT: I just realized that setAttributeNSis 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?

编辑:我刚刚意识到这setAttributeNS很愚蠢,因为那是在更改名称空间,而不是 URI。再次编辑:我有点困惑,当我继续搜索时,我有时会看到命名为soap:"Namespace",所以从这个意义上说,我确实想更改命名空间......但我认为命名空间是“soap”部分。任何澄清?

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.

这是我的第一篇文章,所以如果我问一些已经解决的问题,我深表歉意,但我已经四处搜索,我发现的大部分内容都与更改命名空间有关(例如来自 SOAP-ENV,这是默认命名空间,到soap)而不是URI本身。提前致谢。

-M

-M

回答by erikxiv

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

通常,您不需要手动修改 SOAP 名称空间。您可能想要实现的是创建 SOAP 1.2 消息(其名称空间与 SOAP 1.1 不同)。尝试从代码中删除所有命名空间更改行并将第一行更改为

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");