Spring 集成 Http 出站网关 Header 映射器

Spring Integration Http Outbound Gateway Header Mapper

我正在尝试将自定义 headers 添加到我的 http 出站网关。

我尝试过的事情:

<int:header-enricher>
        <int:header name="replyChannel" value="nullChannel" /> 
        <int:header name="Content-Type" value="application/xml" />
        <int:header name="X-Operation" value="CUSTOM_OPERATION" />
        <int:header name="X-StatusCode" value="200" />
</int:header-enricher>

<int-http:outbound-gateway url="${custom.url}/update"
        mapped-request-headers="HTTP_REQUEST_HEADERS, Operation, StatusCode"        
        http-method="POST" request-factory="serviceRequestFactory"
        expected-response-type="java.lang.String" 
        error-handler="errorChannelHandler" />

在上面的场景中,它可以工作,但我添加的自定义 headers 默认以 X- 为前缀。

为了摆脱它,我尝试了以下但它没有将我的自定义值设置为 headers:

<int:header-enricher>
        <int:header name="replyChannel" value="nullChannel" /> 
        <int:header name="Content-Type" value="application/xml" />
        <int:header name="Operation" value="CUSTOM_OPERATION" />
        <int:header name="StatusCode" value="200" />
</int:header-enricher>

<int-http:outbound-gateway url="${custom.url}/update"
        header-mapper="headerMapper"        
        http-method="POST" request-factory="serviceRequestFactory"
        expected-response-type="java.lang.String" 
        error-handler="errorChannelHandler" />

<bean id="headerMapper" class="org.springframework.integration.http.support.DefaultHttpHeaderMapper">
    <property name="inboundHeaderNames" value="*"/>
    <property name="outboundHeaderNames" value="HTTP_REQUEST_HEADERS, Operation, StatusCode" />
    <property name="userDefinedHeaderPrefix" value=""/>
</bean>

在花了很多时间试图解决这个问题之后,我被卡住了。任何帮助将不胜感激。

我刚刚使用示例应用程序测试了您的映射器,它工作正常...

<int:gateway id="requestGateway" 
             service-interface="org.springframework.integration.samples.http.RequestGateway"
             default-request-channel="requestChannel">
     <int:default-header name="Operation" value="foo" />
     <int:default-header name="StatusCode" value="bar" />
</int:gateway>

<int:channel id="requestChannel"/>

<int-http:outbound-gateway request-channel="requestChannel" 
                           url="http://localhost:18080/http/receiveGateway"
                           http-method="POST"
                           header-mapper="headerMapper"
                           expected-response-type="java.lang.String"/>

<bean id="headerMapper"
    class="org.springframework.integration.http.support.DefaultHttpHeaderMapper">
    <property name="inboundHeaderNames" value="*" />
    <property name="outboundHeaderNames" value="HTTP_REQUEST_HEADERS, Operation, StatusCode" />
    <property name="userDefinedHeaderPrefix" value="" />
</bean>


POST /http/receiveGateway HTTP/1.1
Accept: text/plain, */*
Operation: foo
StatusCode: bar
Content-Type: text/plain;charset=UTF-8
Accept-Charset: big5, ...
User-Agent: Java/1.8.0_60
Host: localhost:8080
Connection: keep-alive
Content-Length: 5

打开 org.springframework.integration 的 DEBUG 日志记录以获得 header 映射的详细日志记录...

11:13:19.562 DEBUG [main][org.springframework.integration.http.support.DefaultHttpHeaderMapper] headerName=[operation] WILL be mapped, matched pattern=operation
11:13:19.562 DEBUG [main][org.springframework.integration.http.support.DefaultHttpHeaderMapper] setting headerName=[Operation], value=foo
11:13:19.563 DEBUG [main][org.springframework.integration.http.support.DefaultHttpHeaderMapper] headerName=[errorchannel] WILL NOT be mapped
11:13:19.563 DEBUG [main][org.springframework.integration.http.support.DefaultHttpHeaderMapper] headerName=[id] WILL NOT be mapped
11:13:19.563 DEBUG [main][org.springframework.integration.http.support.DefaultHttpHeaderMapper] headerName=[statuscode] WILL be mapped, matched pattern=statuscode
11:13:19.563 DEBUG [main][org.springframework.integration.http.support.DefaultHttpHeaderMapper] setting headerName=[StatusCode], value=bar