【问题标题】:Set dynamic custom headers in http-outbound gateway在 http-outbound 网关中设置动态自定义标头
【发布时间】:2017-02-09 05:49:51
【问题描述】:

我有 http 出站网关:

<int-http:outbound-gateway encode-uri="true" http-method="POST" header-mapper="headerMappper"
        charset="UTF-8" url="{url}"  expected-response-type="java.lang.String">
        <int-http:uri-variable name="url" expression="headers.uri"/>
    </int-http:outbound-gateway>

Header Mapper bean 配置:

<bean    class="com.cc.gateway.HeaderMapper"/>

public class HeaderMapper extends org.springframework.integration.http.support.DefaultHttpHeaderMapper{

    @Bean("headerMappper")
    public HeaderMapper mapHeader()
    {
        this.setOutboundHeaderNames(getHeaderMapper());
        this.setUserDefinedHeaderPrefix("");
        return this;
    }

    public String[] getHeaderMapper()
    {
        Object [] headersArray =new HeadersConfig().getHeaders().keySet().toArray();

        return Arrays.copyOf(headersArray,headersArray.length,String[].class);

    }

}

如何为每个请求设置标头映射器配置? 我的配置在部署时只读取一次。

【问题讨论】:

    标签: spring spring-integration


    【解决方案1】:

    DefaultHttpHeaderMapper 不适合这种用途;更改每个请求的映射标头不是线程安全的。

    您必须重写这么多方法以使其成为线程安全的,只实现您自己的自定义HeaderMapper&lt;HttpHeaders&gt; 可能会更容易。

    但是,如果您一次只发送一条消息,只需覆盖 fromHeaders() 并在调用 super.fromHeaders() 之前更新标头以映射。

    也就是说,想要动态更改映射的标头是相当不寻常的。

    【讨论】:

      猜你喜欢
      • 2011-03-15
      • 1970-01-01
      • 2011-03-11
      • 1970-01-01
      • 1970-01-01
      • 2017-01-03
      • 1970-01-01
      • 1970-01-01
      • 2020-05-05
      相关资源
      最近更新 更多