HTTP 輸出元件
本節說明 Spring Integration 的 HTTP 輸出元件。
使用 HttpRequestExecutingMessageHandler
若要設定 HttpRequestExecutingMessageHandler
,請編寫類似於以下的 bean 定義
<bean id="httpOutbound"
class="org.springframework.integration.http.outbound.HttpRequestExecutingMessageHandler">
<constructor-arg value="https://127.0.0.1:8080/example" />
<property name="outputChannel" ref="responseChannel" />
</bean>
此 bean 定義透過委派給 RestTemplate
來執行 HTTP 請求。該範本接著委派給 HttpMessageConverter
實例列表,以從 Message
payload 產生 HTTP 請求 body。您可以設定這些轉換器以及要使用的 ClientHttpRequestFactory
實例,如下列範例所示
<bean id="httpOutbound"
class="org.springframework.integration.http.outbound.HttpRequestExecutingMessageHandler">
<constructor-arg value="https://127.0.0.1:8080/example" />
<property name="outputChannel" ref="responseChannel" />
<property name="messageConverters" ref="messageConverterList" />
<property name="requestFactory" ref="customRequestFactory" />
</bean>
預設情況下,HTTP 請求是透過使用 SimpleClientHttpRequestFactory
的實例產生,它使用 JDK HttpURLConnection
。也支援透過 CommonsClientHttpRequestFactory
使用 Apache Commons HTTP Client,您可以注入它(如先前所示)。
對於輸出閘道,閘道產生的回覆訊息包含請求訊息中存在的所有訊息標頭。 |
使用 Cookie
輸出閘道上的 transfer-cookies
屬性提供基本的 cookie 支援。當設定為 true
(預設為 false
)時,從伺服器在回應中收到的 Set-Cookie
標頭會轉換為回覆訊息中的 Cookie
。此標頭接著用於後續的傳送。這啟用了簡單的狀態互動,例如以下
…→logonGateway→…→doWorkGateway→…→logoffGateway→…
如果 transfer-cookies
為 false
,則收到的任何 Set-Cookie
標頭仍保留為回覆訊息中的 Set-Cookie
,並在後續傳送中丟棄。
空的 Response Body
HTTP 是一種請求-回應協定。但是,回應可能沒有 body,只有標頭。在這種情況下, |
expected-response-type
除了前面關於空 response body 的註解之外,如果回應確實包含 body,您必須提供適當的 |
從 5.5 版開始,HttpRequestExecutingMessageHandler
公開了一個 extractResponseBody
標誌(預設為 true
),以僅傳回 response body,或將整個 ResponseEntity
作為回覆訊息 payload 傳回,而與提供的 expectedResponseType
無關。如果 ResponseEntity
中不存在 body,則會忽略此標誌,並傳回整個 ResponseEntity
。