如何在spring集成中的拦截器上动态设置属性

时间:2016-03-02 12:39:56

标签: spring web-services spring-integration interceptor

我们有一个带有拦截器的webservice出站网关进行一些前后处理,我们需要动态设置拦截器的属性,这与how to pass values through http headers dynamically using spring integration情况非常相似,我们试图实现建议的解决方案,但我们在运行时遇到异常。

这里有相关代码:

拦截器相关代码

public class WebServiceClientInterceptor implements ClientInterceptor {
    private final ThreadLocal<HashMap<String, String>> customHeaders = new ThreadLocal<HashMap<String, String>>();
  .....
    public GenericMessage setCustomHeaders(GenericMessage message, String property, String value) {
        log.info("Adding header");
        if (customHeaders.get() == null) {
            log.info("map was null");
            customHeaders.set(new HashMap<String, String>());
        }
        log.info("set " + property + ": " + value);
        customHeaders.get().put(property, value);
        return message;
    }

上下文定义

<int:chain id="getOperatorTynTecCallChain"
    input-channel="inspector.getoperator.tyntec.enricher.input.channel"
    output-channel="inspector.getoperator.results.enricher.output.channel">
    <int:service-activator expression="@webServiceClientInterceptor.setCustomHeaders(#root, 'mcc' , headers.mcc.getFirst())" />
    <int:transformer method="transformRequest" ref="tynTecInspectorPartnerImpl" />
    <int-ws:header-enricher id="getOperatorTynTecHeaderEnricher">
        <int-ws:soap-action value="urn:SyncSuperQueryService" />
    </int-ws:header-enricher>
    <int-ws:outbound-gateway id="getOperatorTynTecOutboundHttpGateway"
        marshaller="xmlBeansMarshaller" unmarshaller="xmlBeansTynTecUnmarshaller"
        requires-reply="true" message-sender="httpComponentsMessageSender"
        uri="${scoretools.inspector.tyntec.url}"> 
        <interceptor="webServiceClientInterceptor">
    </int-ws:outbound-gateway>

    <int:service-activator expression="@tynTecInspectorPartnerImpl.transformResponse(payload, headers.msisdn, headers.mcc)" />
</int:chain>

<bean id="webServiceClientInterceptor" name="webServiceClientInterceptor"
    class="com.cliqdigital.inspector.util.WebServiceClientInterceptor" />

例外

16:23:56,338 DEBUG [org.springframework.integration.handler.ExpressionEvaluatingMessageProcessor] (default task-1) SpEL Expression evaluation failed with EvaluationException.: org.springframework.expression.spel.SpelEvaluationException: EL1004E:(pos 29): Method call: Method setCustomHeaders() cannot be found on com.sun.proxy.$Proxy52 type

看起来当拦截器被包装在代理中时,方法setCustoHeaders会丢失,任何想法?

1 个答案:

答案 0 :(得分:1)

为什么拦截器被代理?

在任何情况下,您都可以更改拦截器以实现其上具有setCustomHeaders的界面(以及ClientInterceptor)。

代理将公开接口的所有方法。

public class WebServiceClientInterceptor implements ClientInterceptor, CustomHeaderSetter {
    ...
}