将参数传递给 Spring 肥皂拦截器 [英] Pass parameter to Spring soap interceptor
问题描述
我有进行 Web 服务调用的 Spring 集成出站网关.我有一个拦截器拦截soap请求并记录到日志文件.现在我想将一个参数传递给拦截器类的 handleRequest 方法,以便我可以将每个记录的soap请求关联到数据库中的一个id.
I have spring integration outbound gateway that makes web service call. I have a interceptor that intercepts soap request and logs to log file. Now I want to pass a parameter to handleRequest method of interceptor class so that I can associate each logged soap request to an id in database.
我需要传递给拦截器的值 id
存储在名称为 id
的标头中代码如下所示.
The value id
I need to pass to interceptor is stored in headers with name id
code looks something like below.
<int-ws:outbound-gateway uri="${soap.url} interceptor="interceptor" >
</int-ws:outbound-gateway>
下面是拦截器.
public class Interceptor implements ClientInterceptor {
@Override
public boolean handleRequest(MessageContext messageContext) throws WebServiceClientException {
//id below is parameter that needs to be passed.
LOGGER.info("soap request for "+id +" is " +messageContext.getRequest().getDocument().toString());
return true;
}
}
我想过在 bean 定义中为 Interface 设置一个属性.
I thought of setting a property to Interface in the bean definition.
<bean id="interceptor" class="com.services.Interceptor" >
<property name="id" value="#{headers['id']}" />
</bean>
但这不起作用,因为我无法以某种方式访问标题.在服务器启动期间出现异常.. 即使我解决了这个异常,这也会有问题,因为对象是单例.这里还有其他指针吗?
But this is not working as I am unable to access headers here somehow. getting exception during server start up .. Even if I get this exception resolved, this will have issues as object is singleton.Any other pointers here ?
Caused by: org.springframework.expression.spel.SpelEvaluationException: EL1008E:(pos 0): Property or field 'headers' cannot be found on object of type 'org.spri
ngframework.beans.factory.config.BeanExpressionContext' - maybe not public?
at org.springframework.expression.spel.ast.PropertyOrFieldReference.readProperty(PropertyOrFieldReference.java:224)
at org.springframework.expression.spel.ast.PropertyOrFieldReference.getValueInternal(PropertyOrFieldReference.java:94)
at org.springframework.expression.spel.ast.PropertyOrFieldReference.getValueInternal(PropertyOrFieldReference.java:81)
at org.springframework.expression.spel.ast.CompoundExpression.getValueRef(CompoundExpression.java:51)
at org.springframework.expression.spel.ast.CompoundExpression.getValueInternal(CompoundExpression.java:87)
at org.springframework.expression.spel.ast.SpelNodeImpl.getValue(SpelNodeImpl.java:120)
at org.springframework.expression.spel.standard.SpelExpression.getValue(SpelExpression.java:242)
推荐答案
查看我对 你的另一个问题为什么你不能做你正在尝试的事情.
See my answer to your other question for why you can't do what you are trying.
要将参数传递给拦截器,您可以使用 ThreadLocal
- 在网关上游添加服务以设置您的值,然后在拦截器中检索/删除它.
To pass an argument to your interceptor, you could use a ThreadLocal
- add a service upstream of the gateway to set your value and then retrieve/remove it in the interceptor.
这篇关于将参数传递给 Spring 肥皂拦截器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!