【发布时间】:2026-01-23 01:40:02
【问题描述】:
我正在尝试使用 Spring Integration 的 <int-http:outbound-gateway> 和 <int:header-enricher> 组件传递一些自定义 HTTP 标头,但它不适用于我的配置。
我尝试了两种配置。第一个带有“mapped-request-headers”属性的:
<int:chain input-channel="requestChannel" output-channel="replyChannel">
<int:header-enricher>
<int:header name="test" value="test"></int:header>
</int:header-enricher>
<int-http:outbound-gateway id="gateway"
encode-uri="true" url="http://localhost:8080/webapp/service/{request}"
http-method="GET" mapped-request-headers="test, HTTP_REQUEST_HEADERS">
<int-http:uri-variable name="request" expression="payload"/>
</int-http:outbound-gateway>
</int:chain>
第二个带有“header-mapper”属性,相对DefaultHttpHeaderMapper配置:
<int:chain input-channel="requestChannel" output-channel="replyChannel">
<int:header-enricher>
<int:header name="test" value="test"></int:header>
</int:header-enricher>
<int-http:outbound-gateway id="gateway"
encode-uri="true" url="http://localhost:8080/webapp/service/{request}"
http-method="GET" header-mapper="headerMapper">
<int-http:uri-variable name="request" expression="payload"/>
</int-http:outbound-gateway>
</int:chain>
<bean id="headerMapper" class="org.springframework.integration.http.support.DefaultHttpHeaderMapper">
<property name="outboundHeaderNames" value="HTTP_REQUEST_HEADERS, test" />
<property name="userDefinedHeaderPrefix" value="" />
</bean>
但在这两种情况下,远程应用程序加载的消息头如下:
GenericMessage[
payload={},
headers={
http_requestMethod=GET,
replyChannel=org.springframework.messaging.core.GenericMessagingTemplate$TemporaryReplyChannel@4c8b7a27,
errorChannel=org.springframework.messaging.core.GenericMessagingTemplate$TemporaryReplyChannel@4c8b7a27,
host=localhost: 8080,
http_requestUrl=http://localhost:8080/webapp/service/hello,
connection=keep-alive,
id=3c2a21ba-b7f5-e5e9-c821-45251d406318,
cache-control=no-cache,
pragma=no-cache,
user-agent=Java/1.8.0_65,
accept=[
text/html,
image/gif,
image/jpeg,
*/*;q=.2,
*/*;q=.2],
timestamp=1469009855797
}
]
没有我试图添加到请求消息中的“测试”标头。
将自定义标头添加到 <int-http:outbound-gateway> 的正确方法是什么?
任何其他可行的解决方案也很好。
更新
根据 Gary 的指示,我打开了 DEBUG 模式。
这是一件很有趣的事情:日志说header [test] “将被映射”:
message sent: GenericMessage [payload=hello, headers={id=79f98422-418b-f00f-1e21-09461b1ff80c, timestamp=1469021452494}]
2016-07-20 15:53:57 DEBUG org.springframework.integration.http.support.DefaultHttpHeaderMapper.fromHeaders:402 (executor-4) - outboundHeaderNames=[test, HTTP_REQUEST_HEADERS]
2016-07-20 15:53:57 DEBUG org.springframework.integration.http.support.DefaultHttpHeaderMapper.shouldMapHeader:537 (executor-4) - headerName=[test] WILL be mapped, matched pattern=test
2016-07-20 15:53:57 DEBUG org.springframework.integration.http.support.DefaultHttpHeaderMapper.fromHeaders:420 (executor-4) - setting headerName=[X-test], value=test
2016-07-20 15:53:57 DEBUG org.springframework.http.client.support.HttpAccessor.createRequest:79 (executor-4) - Created GET request for "http://localhost:8080/webapp/service/hello"
我在服务器端实现了一个简单的过滤器,它拦截每个 HTTP 请求,并将该请求的每个标头打印在日志上。
这是结果:
x-test=test
cache-control=no-cache
pragma=no-cache
user-agent=Java/1.8.0_65
host=localhost:8080
accept=text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2
connection=keep-alive
因此,标头 "x-test" 出现在 HttpServletRequest 实例上。
但在那之后,我可以在日志中看到另一条消息(我只报告了对这个上下文重要的消息):
2016-07-20 15:53:57 DEBUG org.springframework.web.servlet.DispatcherServlet.doService:865 (http-nio-8080-exec-1) - DispatcherServlet with name 'dispatcherServer' processing GET request for [/webapp/service/hello]
2016-07-20 15:53:57 DEBUG org.springframework.integration.http.support.DefaultHttpHeaderMapper.toHeaders:436 (http-nio-8080-exec-1) - inboundHeaderNames=[Accept, Accept-Charset, Accept-Encoding, Accept-Language, Accept-Ranges, Authorization, Cache-Control, Connection, Content-Length, Content-Type, Cookie, Date, Expect, From, Host, If-Match, If-Modified-Since, If-None-Match, If-Range, If-Unmodified-Since, Max-Forwards, Pragma, Proxy-Authorization, Range, Referer, TE, Upgrade, User-Agent, Via, Warning]
2016-07-20 15:53:57 DEBUG org.springframework.integration.http.support.DefaultHttpHeaderMapper.shouldMapHeader:561 (http-nio-8080-exec-1) - headerName=[x-test] WILL NOT be mapped
2016-07-20 15:53:57 DEBUG org.springframework.integration.handler.AbstractMessageHandler.handleMessage:115 (executor-4) - ServiceActivator for [org.springframework.integration.handler.MethodInvokingMessageProcessor@6ae60a] (channelServiceActivator) received message: GenericMessage [payload={}, headers={http_requestMethod=GET, replyChannel=org.springframework.messaging.core.GenericMessagingTemplate$TemporaryReplyChannel@565fe0d8, errorChannel=org.springframework.messaging.core.GenericMessagingTemplate$TemporaryReplyChannel@565fe0d8, host=localhost:8080, http_requestUrl=http://localhost:8080/webapp/service/hello, connection=keep-alive, id=9578f82d-7fc2-fe94-d8bf-3225ec955b22, cache-control=no-cache, pragma=no-cache, user-agent=Java/1.8.0_65, accept=[text/html, image/gif, image/jpeg, */*;q=.2, */*;q=.2], timestamp=1469022837172}]
现在日志显示标题“[x-test] 不会被映射”。怎么可能?是因为inboundHeaderNames中没有出现?
其实,在 GenericMessage 中,header 似乎已经“消失”了:
GenericMessage [
payload={},
headers={
http_requestMethod=GET,
replyChannel=org.springframework.messaging.core.GenericMessagingTemplate$TemporaryReplyChannel@16b10fb6,
errorChannel=org.springframework.messaging.core.GenericMessagingTemplate$TemporaryReplyChannel@16b10fb6,
host=localhost:8080,
http_requestUrl=http://localhost:8080/webapp/service/hello,
connection=keep-alive,
id=ba2af515-9e29-4d84-d176-5b9d0d066cb0,
cache-control=no-cache,
pragma=no-cache,
user-agent=Java/1.8.0_65,
accept=[text/html,
image/gif,
image/jpeg,
*/*;q=.2,
*/*;q=.2],
timestamp=1469021452542
}
]
我不明白为什么 HTTP 请求中存在标头,但消息通道的标头中不存在。
【问题讨论】: