Spring Integration Http Outbound Gateway Header Mapper

时间:2015-10-23 06:00:53

标签: java spring-integration

我正在尝试将自定义标头添加到我的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" />

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

为了摆脱它,我尝试了以下方法,但它没有将自定义值设置为标题:

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

在花了很多时间试图解决这个问题后,我陷入困境。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:2)

我刚用示例应用程序测试了你的mapper,它工作得很好......

<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日志记录以获取标头映射的详细日志记录...

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