我试图将自定义标头添加到我的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>

在花费大量时间尝试解决此问题后,我陷入了困境。任何帮助,将不胜感激。

最佳答案

我刚刚使用示例应用程序测试了您的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的调试日志记录以获取标头映射的详细日志记录...
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

10-06 09:23