簡體   English   中英

Spring Integration Http Outbound Gateway Header Mapper

[英]Spring Integration Http Outbound Gateway Header Mapper

我正在嘗試將自定義標頭添加到我的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 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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM