2016-07-20 27 views
0

私は、Spring Integrationの<int-http:outbound-gateway><int:header-enricher>コンポーネントを使用してカスタムHTTPヘッダーを渡そうとしていますが、私の設定では動作しません。Spring Integration:HTTPアウトバウンドゲートウェイにカスタムHTTPヘッダーを追加する方法は?

私は2つの構成を試しました。

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

相対DefaultHttpHeaderMapperに構成された「header-mapper」属性を持つ2つ目:

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

しかし、どちらの場合も、メッセージのヘッダがロードされたことにより、「mapped-request-headers」属性を持つ最初のものリモート・アプリケーションは、以下の通りである:

GenericMessage[ 
    payload={}, 
    headers={ 
     http_requestMethod=GET, 
     replyChannel=org.springframewor[email protected]4c8b7a27, 
     errorChannel=org.springframewor[email protected]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>にカスタムヘッダーを追加する正しい方法はありますか?

これは他の作業方法にも適しています。ゲイリー表示からよう

UPDATE

、Iは、デバッグモードをオン。

これは非常に興味深いものです:ログは、ヘッダーと言う[テスト] 「マップされます」:

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-試験"は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.spr[email protected]6ae60a] (channelServiceActivator) received message: GenericMessage [payload={}, headers={http_requestMethod=GET, replyChannel=org.springframewor[email protected]565fe0d8, errorChannel=org.springframewor[email protected]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-テスト]と言いますマップされません "。可能性はどうですか?それはinboundHeaderNamesに表示されないためですか?実際に

、ヘッダはGenericMessageに「消滅」しているように見える:

GenericMessage [ 
    payload={}, 
    headers={ 
     http_requestMethod=GET, 
     replyChannel=org.springframewor[email protected]16b10fb6, 
     errorChannel=org.springframewor[email protected]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

この問題の回答が見つかりました。

<int-http:outbound-gateway>「出」 HTTPリクエストに挿入されなければならなかったヘッダをマッピングします。

<int-http:inbound-gateway>「入る」 HTTPリクエストに含まれているヘッダをマッピングします。

だから、それは、"header-mapper"ことを定義する必要があります。

  • <int-http:inbound-gateway>
  • 私の例の場合、ためのソリューションで<int-http:outbound-gateway>

ため"outboundHeaderNames"をマップするための"inboundHeaderNames"をマップ「OUTBOUND SIDE」(クライアントアプリケーション):

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

は(「テスト」この場合)「発信要求」にマッピングされなければならないヘッダ名とsettedされなければなりません。

「インバウンド側」(受信機アプリケーション)のための溶液であったものの:

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

"inboundHeaderNames"ソース要求のすべてのヘッダがされることを保証「*」にsettedメッセージヘッダーにマップされます。

この方法では、カスタムヘッダーをHTTPリクエストの任意のURLに渡すことができ、メッセージチャネルのコンテキストでサーバー側でカスタムヘッダーを復元できます。

0

DEBUGロギングをオンにします。このようなヘッダーマッピングに関するメッセージが表示されるはずです...

08:46:46:44.987 DEBUG [メイン] ...ヘッダ名= [ID]は

08にマッピングされません44.987 DEBUG [メイン] ...ヘッダ名= [テスト]がされますマッピングされ、マッチしたパターン=テスト

08:46:44.987 DEBUG [メイン] ...ヘッダ名= [X-テスト]、値= FOOを設定

(これはあなたの最初の例である - カスタムヘッダー現在、デフォルトで接頭辞X-が取得されています)。

+0

答えてくれてありがとう、ゲーリー。私はログをオンにして、私はログで気づいた情報で質問を更新しました。ヘッダーがHttpServletRequestインスタンスに存在するが、Messageインスタンスには存在していないことに気付きました。 –

関連する問題