Spring Soap 拦截器如何修改消息的内容? [英] How can a Spring Soap interceptor modify the contents of a message?
问题描述
我正在尝试为 Web 服务编写拦截器,该服务将在发送到端点之前修改 Soap 消息的内容.如果客户端发送了一条消息,其中某个元素的值为 1,我希望能够将该元素更改为 2,这样当消息到达端点时,客户端看起来好像提交了 2 而不是1. 我不确定这是一项让我感到困惑的困难任务,还是一项我做得比它需要的更难的简单任务.
I'm trying to write an interceptor for a web service that will modify the contents of the Soap message before is sent on to the endpoint. If a client sent a message where the value of some element is 1, I want to be able to alter that element to a 2 so that, when the message arrives at the endpoint, it looks as if the client submitted a 2 instead of a 1. I'm not sure if this is a difficult task which is elluding me, or an easy task which I am making harder than it needs to be.
我已经遍历了一些 Spring 拦截器;但是验证和日志拦截器不会改变传输中的消息.Wss4jSecurityInterceptor 确实向 MessageContext 添加了一些属性;但我一直无法利用它正在做的任何事情.我有一个拦截器的外壳;但没有做任何有价值的事情.
I have stepped through some of the Spring interceptors; but the validation and logging interceptors don't every alter the message that is in transit. The Wss4jSecurityInterceptor does add some properties to the MessageContext; but I haven't been able to leverage anything that it is doing. I have a shell of an interceptor; but nothing that is doing anything of any value.
public boolean handleRequest(MessageContext messageContext, Object endpoint)
throws Exception {
SaajSoapMessage saajSoapMessage = (SaajSoapMessage) messageContext
.getRequest();
SOAPMessage soapMessage = saajSoapMessage.getSaajMessage();
SOAPBody soapBody = soapMessage.getSOAPBody();
return true;
}
我希望有其他人已经解决了这个特定问题的机会.任何见解将不胜感激.谢谢.
I was hoping there was a chance that soembody else had already solved this particular problem. Any insight would be appreciated. Thanks.
推荐答案
修改有效负载有点棘手.我发现使这项工作有效的唯一方法是在 SoapBody
上使用 getPayloadSource()
和 getPayloadResult()
方法,它们公开 javax.xml.transform
- 用于操作数据的友好对象.
Modifying the payload is a little bit tricky. The only way I've found to make this work is to use the getPayloadSource()
and getPayloadResult()
methods on SoapBody
, which expose javax.xml.transform
-friendly objects for manipulating the data.
这是令人讨厌的重量级,但你可以这样做:
It's annoyingly heavyweight, but you can do something like this:
Transformer identityTransform = TransformerFactory.newInstance().newTransformer();
DOMResult domResult = new DOMResult();
identityTransform.transform(soapBody.getPayloadSource(), domResult);
Node bodyContent = domResult.getNode(); // modify this
identityTransform.transform(new DOMSource(bodyContent), soapBody.getPayloadResult());
我很想看到一种更好的方式来做到这一点.
I'd love to see a better way of doing this.
这篇关于Spring Soap 拦截器如何修改消息的内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!