在 Spring-Integration 中将 X 附加到自定义请求 Headers

Appending X to Custom Request Headers in Spring-Integration

我发现当我想使用 Spring-Integration 进行 REST 调用时,它会自动附加 'x' 以防它是自定义的 header。

例如,在 Spring-integration 中发送自定义请求 header 时,例如 API-KEY,API 调用中的实际请求 header 名称变为 X-API-KEY 所以它失败了。

似乎 Spring 正在通过强制执行自定义请求 headers 以 X 开头来实现标准化,是否有解决方法?

<int:channel id="requestChannel"/>
<int:channel id="httpHeaderEnricherChannel"/>
<int-http:outbound-gateway request-channel="requestChannel"
                                       url="http://localhost:9090/balance"
                                       http-method="GET"
                                       mapped-request-headers="Api-Key"
                                       expected-response-type="java.lang.String"/>

<int:header-enricher input-channel="httpHeaderEnricherChannel" 
                     output-channel="requestChannel">
    <int:header name="Api-Key" value="pass"/>
</int:header-enricher>

您应该使用 setUserDefinedHeaderPrefix(null) 声明 DefaultHttpHeaderMapper.outboundMapper() bean,并包括您的自定义 Api-Key header 映射。之后,您应该将 mapped-request-headers 属性替换为 header-mapper 引用。

我们修改了该功能并决定在下一版本中删除 "X-" 默认前缀。

有关更多信息,请参阅此处 Custom HTTP headers : naming conventions and here https://jira.spring.io/browse/INT-3903

感谢@Artem 的澄清,感谢 Gary 的 post

我能够解决问题

<int:channel id="requestChannel"/>
<int:gateway id="requestGateway" 
             service-interface="org.springframework.integration.samples.http.RequestGateway"
             default-request-channel="requestChannel">
            <int:default-header name="Api-Key" value="pass" />
</int:gateway>

<int-http:outbound-gateway request-channel="requestChannel"
                           header-mapper="headerMapper"
                           url="http://localhost:9090/balance"
                           http-method="GET"
                           expected-response-type="java.lang.String"/>

<beans:bean id="headerBean"
            class="org.springframework.integration.samples.http.HeaderBean" />

<bean id="headerMapper"
    class="org.springframework.integration.http.support.DefaultHttpHeaderMapper">
    <property name="inboundHeaderNames" value="*" />
    <property name="outboundHeaderNames" value="HTTP_REQUEST_HEADERS, Api-Key" />
    <property name="userDefinedHeaderPrefix" value="" />
</bean>