首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Spring-Integration中将X附加到自定义请求头

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

Stack Overflow用户
提问于 2015-12-07 17:23:29
回答 2查看 1.7K关注 0票数 2

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

例如,在Spring集成中,当发送自定义请求头(如API-KEY )时,API调用中的实际请求标头名称变为X-API-KEY,因此失败。

Spring似乎正在通过强制自定义请求头从X开始标准化,这里有工作吗?

代码语言:javascript
复制
<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>
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-12-07 17:32:22

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

我们已经修改了这个特性,并决定在下一个版本中删除"X-“默认前缀。

有关更多信息,请参见这里的Custom HTTP headers : naming conventionshttps://jira.spring.io/browse/INT-3903

票数 3
EN

Stack Overflow用户

发布于 2015-12-07 19:29:44

感谢@Artem来澄清,并感谢Gary在这里的文章Spring Integration Http Outbound Gateway Header Mapper

我解决了这个问题

代码语言:javascript
复制
<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>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34139501

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档