Spring集成:如何将自定义HTTP标头添加到HTTP出站网关?

时间:2016-07-20 10:36:35

标签: spring spring-integration

我正在尝试使用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模式。

这是一件非常有趣的事情:日志说标题[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中“消失了”:

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

2 个答案:

答案 0 :(得分:2)

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

<int-http:outbound-gateway>会映射必须插入 &#34;退出&#34; HTTP请求的标头。

<int-http:inbound-gateway>会映射 &#34;输入&#34; HTTP请求中包含的标头。

因此,必须将"header-mapper"定义为:

  • 映射"inboundHeaderNames"
  • <int-http:inbound-gateway>
  • 映射"outboundHeaderNames"
  • <int-http:outbound-gateway>

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

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

必须使用必须在&#34;出站请求&#34;中映射的标题名称设置"outboundHeaderNames"。 (&#34; test&#34; 在这种情况下)。

虽然&#34; INBOUND SIDE&#34; (接收器应用程序)的解决方案是:

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

设置为&#34; *&#34; "inboundHeaderNames"确保源请求的所有标头都将映射到邮件标头

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

答案 1 :(得分:0)

启用DEBUG日志记录;您应该看到有关标题映射的消息,如此...

  

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

     

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

     

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

(这是您的第一个示例 - 默认情况下,自定义标头目前获得X-前缀。