【问题标题】:Spring Integration: how to add custom HTTP headers to the HTTP outbound gateway?Spring Integration:如何将自定义 HTTP 标头添加到 HTTP 出站网关?
【发布时间】: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
    }
]

没有我试图添加到请求消息中的“测试”标头

将自定义标头添加到 &lt;int-http:outbound-gateway&gt; 的正确方法是什么?

任何其他可行的解决方案也很好。

更新

根据 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 请求中存在标头,但消息通道的标头中不存在。

【问题讨论】:

    标签: spring spring-integration


    【解决方案1】:

    我已经找到了这个问题的答案。

    &lt;int-http:outbound-gateway&gt; 映射必须插入到 “exiting” HTTP 请求中的标头。

    &lt;int-http:inbound-gateway&gt; 映射 “进入” HTTP 请求中包含的标头。

    所以,它必须定义为"header-mapper"

    • "inboundHeaderNames" 映射为&lt;int-http:inbound-gateway&gt;
    • "outboundHeaderNames" 映射为&lt;int-http:outbound-gateway&gt;

    在我的示例中,"OUTBOUND SIDE"(客户端应用程序)的解决方案是:

    <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>
    

    "outboundHeaderNames" 必须设置为必须在“出站请求”(“测试” 在这种情况下)中映射的标头名称。

    “INBOUND SIDE”(接收方应用程序)的解决方案是:

    <int-http:inbound-gateway id="gateway" request-channel="requestChannel"
          path="/service/**" supported-methods="GET"
          reply-channel="outputChannel" header-mapper="headerMapper">
    </int-http:inbound-gateway>
    
    <bean id="headerMapper" class="org.springframework.integration.http.support.DefaultHttpHeaderMapper">
        <property name="inboundHeaderNames" value="*" />
    </bean>
    

    "inboundHeaderNames" 设置为 "*" 确保源请求的所有标头都将映射到消息标头中

    使用这种方法,我们可以将自定义标头传递给 HTTP 请求中的任何 URL,并且我们可以在消息通道上下文中的服务器端恢复它们。

    【讨论】:

      【解决方案2】:

      开启调试日志;你应该会看到这样的关于标头映射的消息...

      08:46:44.987 DEBUG [main] ... headerName=[id] 不会被映射

      08:46:44.987 DEBUG [main] ... headerName=[test] 将被映射,匹配模式=test

      08:46:44.987 DEBUG [main] ...设置 headerName=[X-test], value=foo

      (这是您的第一个示例 - 自定义标头当前默认获取 X- 前缀)。

      【讨论】:

      • 谢谢你的回答,加里。我已打开日志,并使用我在日志中注意到的信息更新了问题。我注意到标头存在于 HttpServletRequest 实例中,但不在 Message 实例中。