将 SoapHeader 添加到 org.springframework.ws.WebServiceMessage
如何将对象添加到 org.springframework.ws.WebServiceMessage
的肥皂头中
这是我希望最终得到的结构:
<soap:Header>
<credentials xmlns="http://example.com/auth">
<username>username</username>
<password>password</password>
</credentials>
</soap:Header>
How can I add an object into the soap header of a org.springframework.ws.WebServiceMessage
This is the structure I'm looking to end up with:
<soap:Header>
<credentials xmlns="http://example.com/auth">
<username>username</username>
<password>password</password>
</credentials>
</soap:Header>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
基本上,您需要使用
WebServiceMessageCallback
在您的客户端中,用于在消息创建之后、发送之前修改消息。 @skaffman 已经非常准确地描述了其余的代码,因此整个内容可能如下所示:就我个人而言,我发现 Spring-WS 很难满足这样的基本需求,他们应该修复 SWS-479。
Basically, you need to use a
WebServiceMessageCallback
in your client to modify the message after its creation but before it is sent. To rest of the code has been described pretty accurately by @skaffman so the whole stuff might look like this:Personally, I find that Spring-WS sucks hard for such a basic need, they should fix SWS-479.
您可以执行以下操作:
然后
You can do as below:
Then
您需要将
WebServiceMessage
转换为SoapMessage
,它具有可用于修改标头的getSoapHeader()
方法。反过来,SoapHeader 有各种添加元素的方法,包括 getResult()(可以用作 Transformer.transform() 的输出) > 操作)。You need to cast the
WebServiceMessage
toSoapMessage
, which has agetSoapHeader()
method you can use to modify the header. In turn,SoapHeader
has various methods for adding elements, includinggetResult()
(which can be used as the output of aTransformer.transform()
operation).您也可以通过创建子元素的键值映射来实现:
在soap标头中设置子元素的命名空间和前缀:
然后,您可以调用WebServiceTemplate的方法
marshalSendAndReceive
,您可以在其中重写 WebServiceMessageCallback 的方法 doWithMessage 如下:上述步骤导致:
You can achieve it by creating key-value map of child elements as well:
Set up namespace and prefix of your child element within the soap header:
Then, you can invoke WebServiceTemplate's method
marshalSendAndReceive
where you override WebServiceMessageCallback's method doWithMessage as follows:The above steps result in:
Response response = (Response)getWebServiceTemplate() .marshalSendAndReceive(request, new HeaderModifier());
创建类 HeaderModifier 并重写 doWithMessage
Response response = (Response)getWebServiceTemplate() .marshalSendAndReceive(request, new HeaderModifier());
Create class HeaderModifier and override doWithMessage