|
如需使用最新稳定版本,请使用 Spring Integration 7.0.4! |
HTTP 出站组件
本节介绍 Spring Integration 的 HTTP 出站组件。
使用HttpRequestExecutingMessageHandler
要配置HttpRequestExecutingMessageHandler,请编写如下所示的 Bean 定义:
<bean id="httpOutbound"
class="org.springframework.integration.http.outbound.HttpRequestExecutingMessageHandler">
<constructor-arg value="http://localhost:8080/example" />
<property name="outputChannel" ref="responseChannel" />
</bean>
此 Bean 定义通过将 HTTP 请求委托给 RestTemplate 来运行。
该模板随后将请求委托给一系列 HttpMessageConverter 实例,以从 Message 有效负载生成 HTTP 请求体。
正如以下示例所示,您也可以配置这些转换器以及要使用的 ClientHttpRequestFactory 实例:
<bean id="httpOutbound"
class="org.springframework.integration.http.outbound.HttpRequestExecutingMessageHandler">
<constructor-arg value="http://localhost: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
基本的 Cookie 支持由出站网关上的 transfer-cookies 属性提供。
当设置为 true(默认为 false)时,从服务器在响应中接收到的 Set-Cookie 头将被转换为回复消息中的 Cookie。
随后,该头将在后续发送中使用。
这实现了简单的有状态交互,如下所示:
…→logonGateway→…→doWorkGateway→…→logoffGateway→…
如果 transfer-cookies 是 false,则任何接收到的 Set-Cookie 标头在回复消息中保持为 Set-Cookie,并在后续发送时被丢弃。
|
空响应体
HTTP 是一种请求 - 响应协议。
然而,响应可能没有主体(body),仅包含头部(headers)。
在这种情况下, |
|
expected-response-type
关于前面提到的空响应体,如果响应确实包含一个体,则必须提供适当的 |
从版本 5.5 开始,HttpRequestExecutingMessageHandler 暴露了一个 extractResponseBody 标志(默认为 true),用于仅返回响应体,或无论提供的 expectedResponseType 如何,都返回整个 ResponseEntity 作为回复消息负载。
如果 ResponseEntity 中不存在响应体,则忽略此标志并返回整个 ResponseEntity。