本文介绍了Spring WS客户端添加了SoapHeader的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用soap标头发送SOAP请求,如下所示:

I'm trying to send SOAP request with soap header looks like this:

<SOAP-ENV:Header>
<Security xmlns="http://www.xxx.org/xxx/2003/05">
<UsernameToken><Username>yyyy</Username><Password>xxx</Password>
</UsernameToken></Security></SOAP-ENV:Header>

为了做到这一点,我使用 SoapActionCallback添加标题元素

In order to do it I'm adding header element using SoapActionCallback

SoapActionCallback actionCallBack = new SoapActionCallback("https://aaa.com/bbb.asmx") {
            public void doWithMessage(WebServiceMessage msg) {
                SoapMessage smsg = (SoapMessage) msg;
                smsg.setSoapAction("http://www.xxx.org/yyy/2003/05/SessionCreate");
                SoapHeaderElement security = smsg.getSoapHeader().addHeaderElement(new QName("http://www.xxx.org/yyy", "Security"));
                security.setText("<UsernameToken><Username>yyyy</Username><Password>xxx</Password></UsernameToken>");
            }
        };

我的问题是soap标题看起来像这样

My problem is that soap header looks like this

<SOAP-ENV:Header><Security xmlns="http://www.xxx.org/yyy/2003/05">&lt;UsernameToken&gt;&lt;Username&gt;yyyyy&lt;/Username&gt;&lt;Password&gt;xxxx&lt;/Password&gt;&lt;/UsernameToken&gt;</Security></SOAP-ENV:Header>

结果我的请求失败:

如何正确添加此消息?

How can I add this message correct?

推荐答案

我也想要同样的事情。但是,正如我在自己的搜索中看到的那样,Spring没有像java中的SOAPMessage那样的完整函数。你可能需要的是一个addChildElement()到标题。

I want the same thing as well. However, as it appears in my own searching, Spring doesn't have full functions like the SOAPMessage in java. What you probably needed was an addChildElement() to the header.

我最终放弃了SoapMessage大楼,并继续这样做:

I ended up ditching the SoapMessage building and went with this:

SOAPMessage message = MessageFactory.newInstance();
private void buildHeader(String userName, String password) throws SOAPException{
    SOAPHeader header = message.getSOAPHeader();
    QName authHeader = new QName(SCHEMA, "AuthenticationHeader", SCHEMA_PREFIX);
    SOAPHeaderElement authElement = header.addHeaderElement(authHeader);

    QName userNameHeader = new QName(SCHEMA, "UserName", SCHEMA_PREFIX);
    SOAPElement userElement = authElement.addChildElement((userNameHeader));
    userElement.setTextContent(userName);

    QName passwordHeader = new QName(SCHEMA, "Password", SCHEMA_PREFIX);
    SOAPElement passwdElement = authElement.addChildElement(passwordHeader);
    passwdElement.setTextContent(password);


}

请注意上述方法通过使用sendSourceAndReceiveToResult的SOAPMessage将SOAPMessage包装在另一个信封中,这不是我们想要的。 :(

Do note that with the approach above passing a SOAPMessage using sendSourceAndReceiveToResult wraps the SOAPMessage in another envelope and this is not what we want. :(

更新(2013年10月25日):我发现

Update (10/25/2013): I found this solution in another discussion. It's a little vague but worth looking into. I'll try to test this out on my end. One of my colleagues tried out spring-integration-ws and looking in google for the topic and soap headers yielded some promising resources.

更新(2013年10月25日):在鬼混之后我发现使用这个库可以更快地将SOAPMessage包装到Spring的SoapMessage中:

Update (10/25/2013): After fooling around with the library I discovered there is a quicker way to wrap the SOAPMessage into Spring's SoapMessage using this:

// message is a SOAPMessage object with custom headers
SoapMessage soapMessage = new SaajSoapMessage(message);
soapMessage.writeTo(System.out);

也许在你建立肥皂消息后,你可以把它送到春天等等。

Perhaps after you build your soapmessage you can route it to spring-ws and so on.

这篇关于Spring WS客户端添加了SoapHeader的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!