在Spring-WS中将子元素添加到自定义SOAP头中 [英] Add child elements to custom SOAP header in Spring-WS

查看:745
本文介绍了在Spring-WS中将子元素添加到自定义SOAP头中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用Spring-WS调用SOAP webservice。有问题的webservice要求我在SOAP头中传递一些信息,如下所示:

I am calling a SOAP webservice with Spring-WS. The webservice in question requires me to pass some information in the SOAP header as shown here:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
   <soapenv:Header>
      <CustomHeaderElement>
         <clientID>xyz</clientID>
         <wsdlVersion>1.0</wsdlVersion>
         <serviceType>ExampleService_v1</serviceType>
      </CustomHeaderElement>
   </soapenv:Header>
   <soapenv:Body>
   ...
   </soapenv:Body>
</soapenv:Envelope>

我已经想出了如何拥有顶层的CustomHeaderElement,但是我没有看到任何东西Spring-WS API允许我添加一个子元素。这是我到目前为止:

I've figured out how to had the top level CustomHeaderElement, but I don't see anything in the Spring-WS API that allows me to add a child element. Here is what I have so far:

WebServiceTemplate template = ...;

template.marshalSendAndReceive(request, new WebServiceMessageCallback(){
    public void doWithMessage(WebServiceMessage message) throws IOException, TransformerException{
        SoapMessage soapMessage = (SoapMessage)message;
        SoapHeader soapHeader = soapMessage.getSoapHeader();
        QName qName = new QName("CustomHeaderElement");
        SOAPHeaderElement headerElement = soapHeader.addHeaderElement(qName);
        //would like to do something like headerElement.addChild(clientIdNode);
    }
});

问题是headerElement似乎没有公开任何实际添加子项的方法。我知道我可以添加一个属性,但这不是我需要的这个服务调用。有人知道我怎么可以将必要的子元素添加到我的自定义标题中吗?

The problem is headerElement doesn't seem to expose any means of actually adding a child. I know I can add an attribute, but that's not what I need for this service call. Does anyone know how I could add the necessary child elements to my custom header?

推荐答案

我对此解决方案不满意,但事实证明,您实际上可以将消息转换为SOAPMessage,从而使您可以完全访问所有SAAJ API。从那里你可以在标题内建立你想要的任何元素。

I'm not pleased with this solution, but as it turns out you can actually cast the message to a SOAPMessage which gives you full access to all the SAAJ apis. From there you can build whatever elements you want inside the header.

这篇关于在Spring-WS中将子元素添加到自定义SOAP头中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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