我发现,当我想使用Spring进行REST调用时,它会自动添加'x‘,以防它是自定义的头。
例如,在Spring集成中,当发送自定义请求头(如API-KEY )时,API调用中的实际请求标头名称变为X-API-KEY,因此失败。
Spring似乎正在通过强制自定义请求头从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>发布于 2015-12-07 17:32:22
您应该使用DefaultHttpHeaderMapper.outboundMapper()声明setUserDefinedHeaderPrefix(null),并包含定制的Api-Key头映射。之后,您应该将mapped-request-headers属性替换为header-mapper引用。
我们已经修改了这个特性,并决定在下一个版本中删除"X-“默认前缀。
有关更多信息,请参见这里的Custom HTTP headers : naming conventions和https://jira.spring.io/browse/INT-3903。
发布于 2015-12-07 19:29:44
感谢@Artem来澄清,并感谢Gary在这里的文章Spring Integration Http Outbound Gateway Header Mapper
我解决了这个问题
<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>https://stackoverflow.com/questions/34139501
复制相似问题