Spring Integration如何实现HTTP协议的集成支持?

2026-05-23 22:071阅读0评论SEO资讯
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计1576个文字,预计阅读时间需要7分钟。

Spring Integration如何实现HTTP协议的集成支持?

Spring Integration 支持运行和处理 HTTP 请求。HTTP 支持由以下网关实现组成:和。另外,请参阅 WebFlux 支持的相关内容。HttpInboundEndpoint 和 HttpRequestExecutingMessageHandler 用于处理 HTTP 请求。

Spring Integration的HTTP支持允许运行HTTP请求和处理入站HTTP请求。 HTTP 支持由以下网关实现组成:和 。 另请参阅WebFlux 支持​。​​HttpInboundEndpoint​​​​HttpRequestExecutingMessageHandler​​

Spring Integration如何实现HTTP协议的集成支持?

您需要将此依赖项包含在项目中:

<dependency> <groupId>org.springframework.integration</groupId> <artifactId>spring-integration-localhost:8080/example" /> <property name="outputChannel" ref="responseChannel" /></bean>

此 Bean 定义通过委托给 . 反过来,该模板委托给实例列表,以从有效负载生成 HTTP 请求正文。 您可以配置这些转换器以及要使用的实例,如以下示例所示:​​RestTemplate​​​​HttpMessageConverter​​​​Message​​​​ClientHttpRequestFactory​​

<bean id="localhost:8080/example" /> <property name="outputChannel" ref="responseChannel" /> <property name="messageConverters" ref="messageConverterList" /> <property name="requestFactory" ref="customRequestFactory" /></bean>

缺省情况下,HTTP 请求是使用 的实例生成的,该实例使用 JDK。 Apache Commons HTTP 客户端的使用也支持通过 ,您可以注入该客户端(如前所示)。​​SimpleClientHttpRequestFactory​​​​HttpURLConnection​​​​CommonsClientHttpRequestFactory​​

对于出站网关,网关生成的回复消息包含请求消息中存在的所有消息标头。

使用饼干

基本 Cookie 支持由出站网关上的属性提供。 设置为 (默认值为 ) 时,在响应中从服务器接收的标头将转换为回复消息中的标头。 然后,此标头将用于后续发送。 这样可以实现简单的有状态交互,如下所示:​​transfer-cookies​​​​true​​​​false​​​​Set-Cookie​​​​Cookie​​

​​…→logonGateway→…→doWorkGateway→…→logoffGateway→…​​

如果是,则收到的任何标头都保持与回复消息中一样,并在后续发送时删除。​​transfer-cookies​​​​false​​​​Set-Cookie​​​​Set-Cookie​​

空响应正文

HTTP 是一种请求-响应协议。 但是,响应可能没有正文,只有标头。 在这种情况下,生成的回复有效负载为 ,无论提供任何 . 根据HTTP RFC 状态代码定义​,有许多状态要求响应不得包含消息正文(例如,)。 在某些情况下,对同一 URL 的调用可能会也可能不会返回响应正文。 例如,对 HTTP 资源的第一个请求返回内容,但第二个请求不返回内容(返回 )。 但是,在所有情况下,都会填充邮件头。 这可以在 HTTP 出站网关之后的某些路由逻辑中使用。 您还可以使用“有效负载类型路由器/>”<将带有 a 的消息路由到与用于正文响应的消息不同的流。​​HttpRequestExecutingMessageHandler​​​​Message​​​​org.springframework.www.springframework.org/schema/beans" xmlns:xsi="www.w3.org/2001/XMLSchema-instance" xmlns:int="www.springframework.org/schema/integration" xmlns:int-www.springframework.org/schema/integration/www.springframework.org/schema/beans www.springframework.org/schema/beans/spring-beans.xsd www.springframework.org/schema/integration www.springframework.org/schema/integration/spring-integration.xsd www.springframework.org/schema/integration/www.springframework.org/schema/integration/localhost/test" localhost/test" localhost/example" local.yahooapis.com/trafficData?appid=YdnDemo&zip={zipCode}" request-channel="trafficChannel" foo.host/{foo}/bars/{bar}" request-channel="trafficChannel" HOST:PORT/PATH') .queryParams(payload) .build() .toUri()"

该方法需要 a 作为参数,因此您可以在执行请求之前提前构建一组真实的 URL 查询参数。​​queryParams()​​​​MultiValueMap<String, String>​​

整体也可以表示为 ,如以下示例所示:​​queryString​​​​uri-variable​​

<int-testServer/test?{queryString}"> <int-somehost/%2f/fooApps?bar={param}" encoding-mode="NONE"> <localhost:8080/foo"); handler.setHttpMethod(HttpMethod.POST); handler.setExpectedResponseType(String.class); return handler;}

以下示例显示如何使用 Java DSL 配置出站网关:

例 4.使用 Java DSL 的出站网关

@Beanpublic IntegrationFlow outbound() { return IntegrationFlow.from("localhost:8080/foo") .samples.openweathermap.org/data/2.5/weather?q={city}" docs.oracle.com/javase/8/docs/technotes/guides/net/proxies.html

春天的​​SimpleClientHttpRequestFactory​​

如果您需要对代理配置进行更明确的控制,则可以使用 Spring 并配置其“代理”属性,如以下示例所示:​​SimpleClientHttpRequestFactory​​

<bean id="requestFactory" class="org.springframework.localhost/test2" mapped-request-headers="thing1, thing2" mapped-response-headers="X-*, HTTP_RESPONSE_HEADERS" channel="someChannel"/><int-localhost/test2" mapped-request-headers="thing1, thing2, HTTP_REQUEST_HEADERS" channel="someChannel"/>

适配器和网关使用 ,它现在为入站和出站适配器提供了两种静态工厂方法,以便可以应用正确的方向(根据需要将 HTTP 请求和响应映射入或映射出)。​​DefaultHttpHeaderMapper​​

如果需要进一步自定义,还可以单独配置 ,并通过属性将其注入适配器。​​DefaultHttpHeaderMapper​​​​header-mapper​​

在 5.0 版之前,用户定义的非标准 HTTP 标头的默认前缀为 。 版本 5.0 将默认前缀更改为空字符串。 根据RFC-6648,现在不鼓励使用此类前缀。 您仍可以通过设置属性来自定义此选项。 以下示例为 HTTP 网关配置标头映射器:​​DefaultHttpHeaderMapper​​​​X-​​​​DefaultHttpHeaderMapper.setUserDefinedHeaderPrefix()​​

<int-localhost/test2" header-mapper="headerMapper" channel="someChannel"/><bean id="headerMapper" class="o.s.i.localhost:8080/multipart-http/inboundAdapter.htm";Resource s2logo = new ClassPathResource("org/springframework/samples/multipart/spring09_logo.png");MultiValueMap map = new LinkedMultiValueMap();map.add("company", "SpringSource");map.add("company-logo", s2logo);HttpHeaders headers = new HttpHeaders();headers.setContentType(new MediaType("multipart", "form-data"));HttpEntity request = new HttpEntity(map, headers);ResponseEntity<?> httpResponse = template.exchange(uri, HttpMethod.POST, request, null);

这就是我们对客户所需要的一切。

在服务器端,我们有以下配置:

<int-http:inbound-channel-adapter id="httpInboundAdapter" channel="receiveChannel" path="/inboundAdapter.htm" supported-methods="GET, POST"/><int:channel id="receiveChannel"/><int:service-activator input-channel="receiveChannel"> <bean class="org.springframework.integration.samples.multipart.MultipartReceiver"/></int:service-activator><bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"/>

“httpInboundAdapter”接收请求并将其转换为有效负载为 . 然后,我们在“multipartReceiver”服务激活器中解析它,如以下示例所示:​​Message​​​​LinkedMultiValueMap​​

public void receive(LinkedMultiValueMap<String, Object> multipartRequest){ System.out.println("### Successfully received multipart request ###"); for (String elementName : multipartRequest.keySet()) { if (elementName.equals("company")){ System.out.println("\t" + elementName + " - " + ((String[]) multipartRequest.getFirst("company"))[0]); } else if (elementName.equals("company-logo")){ System.out.println("\t" + elementName + " - as UploadedMultipartFile: " + ((UploadedMultipartFile) multipartRequest .getFirst("company-logo")).getOriginalFilename()); } }}

应会看到以下输出:

### Successfully received multipart request ### company - SpringSource company-logo - as UploadedMultipartFile: spring09_logo.png
标签:HTTP支持S

本文共计1576个文字,预计阅读时间需要7分钟。

Spring Integration如何实现HTTP协议的集成支持?

Spring Integration 支持运行和处理 HTTP 请求。HTTP 支持由以下网关实现组成:和。另外,请参阅 WebFlux 支持的相关内容。HttpInboundEndpoint 和 HttpRequestExecutingMessageHandler 用于处理 HTTP 请求。

Spring Integration的HTTP支持允许运行HTTP请求和处理入站HTTP请求。 HTTP 支持由以下网关实现组成:和 。 另请参阅WebFlux 支持​。​​HttpInboundEndpoint​​​​HttpRequestExecutingMessageHandler​​

Spring Integration如何实现HTTP协议的集成支持?

您需要将此依赖项包含在项目中:

<dependency> <groupId>org.springframework.integration</groupId> <artifactId>spring-integration-localhost:8080/example" /> <property name="outputChannel" ref="responseChannel" /></bean>

此 Bean 定义通过委托给 . 反过来,该模板委托给实例列表,以从有效负载生成 HTTP 请求正文。 您可以配置这些转换器以及要使用的实例,如以下示例所示:​​RestTemplate​​​​HttpMessageConverter​​​​Message​​​​ClientHttpRequestFactory​​

<bean id="localhost:8080/example" /> <property name="outputChannel" ref="responseChannel" /> <property name="messageConverters" ref="messageConverterList" /> <property name="requestFactory" ref="customRequestFactory" /></bean>

缺省情况下,HTTP 请求是使用 的实例生成的,该实例使用 JDK。 Apache Commons HTTP 客户端的使用也支持通过 ,您可以注入该客户端(如前所示)。​​SimpleClientHttpRequestFactory​​​​HttpURLConnection​​​​CommonsClientHttpRequestFactory​​

对于出站网关,网关生成的回复消息包含请求消息中存在的所有消息标头。

使用饼干

基本 Cookie 支持由出站网关上的属性提供。 设置为 (默认值为 ) 时,在响应中从服务器接收的标头将转换为回复消息中的标头。 然后,此标头将用于后续发送。 这样可以实现简单的有状态交互,如下所示:​​transfer-cookies​​​​true​​​​false​​​​Set-Cookie​​​​Cookie​​

​​…→logonGateway→…→doWorkGateway→…→logoffGateway→…​​

如果是,则收到的任何标头都保持与回复消息中一样,并在后续发送时删除。​​transfer-cookies​​​​false​​​​Set-Cookie​​​​Set-Cookie​​

空响应正文

HTTP 是一种请求-响应协议。 但是,响应可能没有正文,只有标头。 在这种情况下,生成的回复有效负载为 ,无论提供任何 . 根据HTTP RFC 状态代码定义​,有许多状态要求响应不得包含消息正文(例如,)。 在某些情况下,对同一 URL 的调用可能会也可能不会返回响应正文。 例如,对 HTTP 资源的第一个请求返回内容,但第二个请求不返回内容(返回 )。 但是,在所有情况下,都会填充邮件头。 这可以在 HTTP 出站网关之后的某些路由逻辑中使用。 您还可以使用“有效负载类型路由器/>”<将带有 a 的消息路由到与用于正文响应的消息不同的流。​​HttpRequestExecutingMessageHandler​​​​Message​​​​org.springframework.www.springframework.org/schema/beans" xmlns:xsi="www.w3.org/2001/XMLSchema-instance" xmlns:int="www.springframework.org/schema/integration" xmlns:int-www.springframework.org/schema/integration/www.springframework.org/schema/beans www.springframework.org/schema/beans/spring-beans.xsd www.springframework.org/schema/integration www.springframework.org/schema/integration/spring-integration.xsd www.springframework.org/schema/integration/www.springframework.org/schema/integration/localhost/test" localhost/test" localhost/example" local.yahooapis.com/trafficData?appid=YdnDemo&zip={zipCode}" request-channel="trafficChannel" foo.host/{foo}/bars/{bar}" request-channel="trafficChannel" HOST:PORT/PATH') .queryParams(payload) .build() .toUri()"

该方法需要 a 作为参数,因此您可以在执行请求之前提前构建一组真实的 URL 查询参数。​​queryParams()​​​​MultiValueMap<String, String>​​

整体也可以表示为 ,如以下示例所示:​​queryString​​​​uri-variable​​

<int-testServer/test?{queryString}"> <int-somehost/%2f/fooApps?bar={param}" encoding-mode="NONE"> <localhost:8080/foo"); handler.setHttpMethod(HttpMethod.POST); handler.setExpectedResponseType(String.class); return handler;}

以下示例显示如何使用 Java DSL 配置出站网关:

例 4.使用 Java DSL 的出站网关

@Beanpublic IntegrationFlow outbound() { return IntegrationFlow.from("localhost:8080/foo") .samples.openweathermap.org/data/2.5/weather?q={city}" docs.oracle.com/javase/8/docs/technotes/guides/net/proxies.html

春天的​​SimpleClientHttpRequestFactory​​

如果您需要对代理配置进行更明确的控制,则可以使用 Spring 并配置其“代理”属性,如以下示例所示:​​SimpleClientHttpRequestFactory​​

<bean id="requestFactory" class="org.springframework.localhost/test2" mapped-request-headers="thing1, thing2" mapped-response-headers="X-*, HTTP_RESPONSE_HEADERS" channel="someChannel"/><int-localhost/test2" mapped-request-headers="thing1, thing2, HTTP_REQUEST_HEADERS" channel="someChannel"/>

适配器和网关使用 ,它现在为入站和出站适配器提供了两种静态工厂方法,以便可以应用正确的方向(根据需要将 HTTP 请求和响应映射入或映射出)。​​DefaultHttpHeaderMapper​​

如果需要进一步自定义,还可以单独配置 ,并通过属性将其注入适配器。​​DefaultHttpHeaderMapper​​​​header-mapper​​

在 5.0 版之前,用户定义的非标准 HTTP 标头的默认前缀为 。 版本 5.0 将默认前缀更改为空字符串。 根据RFC-6648,现在不鼓励使用此类前缀。 您仍可以通过设置属性来自定义此选项。 以下示例为 HTTP 网关配置标头映射器:​​DefaultHttpHeaderMapper​​​​X-​​​​DefaultHttpHeaderMapper.setUserDefinedHeaderPrefix()​​

<int-localhost/test2" header-mapper="headerMapper" channel="someChannel"/><bean id="headerMapper" class="o.s.i.localhost:8080/multipart-http/inboundAdapter.htm";Resource s2logo = new ClassPathResource("org/springframework/samples/multipart/spring09_logo.png");MultiValueMap map = new LinkedMultiValueMap();map.add("company", "SpringSource");map.add("company-logo", s2logo);HttpHeaders headers = new HttpHeaders();headers.setContentType(new MediaType("multipart", "form-data"));HttpEntity request = new HttpEntity(map, headers);ResponseEntity<?> httpResponse = template.exchange(uri, HttpMethod.POST, request, null);

这就是我们对客户所需要的一切。

在服务器端,我们有以下配置:

<int-http:inbound-channel-adapter id="httpInboundAdapter" channel="receiveChannel" path="/inboundAdapter.htm" supported-methods="GET, POST"/><int:channel id="receiveChannel"/><int:service-activator input-channel="receiveChannel"> <bean class="org.springframework.integration.samples.multipart.MultipartReceiver"/></int:service-activator><bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"/>

“httpInboundAdapter”接收请求并将其转换为有效负载为 . 然后,我们在“multipartReceiver”服务激活器中解析它,如以下示例所示:​​Message​​​​LinkedMultiValueMap​​

public void receive(LinkedMultiValueMap<String, Object> multipartRequest){ System.out.println("### Successfully received multipart request ###"); for (String elementName : multipartRequest.keySet()) { if (elementName.equals("company")){ System.out.println("\t" + elementName + " - " + ((String[]) multipartRequest.getFirst("company"))[0]); } else if (elementName.equals("company-logo")){ System.out.println("\t" + elementName + " - as UploadedMultipartFile: " + ((UploadedMultipartFile) multipartRequest .getFirst("company-logo")).getOriginalFilename()); } }}

应会看到以下输出:

### Successfully received multipart request ### company - SpringSource company-logo - as UploadedMultipartFile: spring09_logo.png
标签:HTTP支持S