如何传递多个动态标头和请求正文以在 Spring 集成中进行休息调用
MyGateway(在服务接口中定义)是一个接口,将包含一个请求正文(JsonFormat),我将在 http-outbound-gateway 中将其传递给其余调用
<!-- Gateway -->
<int:gateway id="requestGateway"
service-interface="com.spring.example.MyGateway"
default-request-channel="requestChannel12345"/>
<int:channel id="requestChannel12345"/>
<bean id="methodInsideService" class="com.spring.example.ServiceImpl"/>
使用下面的 Service-Activator I创建了一个模型类,其中包含标头的值,以便我可以在进行休息调用时使用动态标头 --->下面
<int:service-activator input-channel="requestChannel12345"
output-channel="responseChannel1234567"
ref="methodInsideService"
method="methodExample"/>
<bean id="clientHttpRequestFactory" class="org.springframework.http.client.HttpComponentsClientHttpRequestFactory"/>
这里我接受请求通道(responseChannel1234567)作为动态标头响应的outpt-channel(responseChannel1234567) --->这里我的实际 json 请求被我创建的标头有效负载覆盖,以将其用作动态标头值
<int:chain input-channel="responseChannel1234567" >
<int:header-enricher>
<int:header name="header1" expression="payload.getheader1()" ></int:header>
<int:header name="header2" expression="payload.getheader2()" ></int:header>
<int:header name="header3" expression="payload.getheader3()" ></int:header>
</int:header-enricher>
<int-http:outbound-gateway
id="requestGateway"
url="http://localhost/test?queryParam1={queryParam1}&queryParam2={queryParam2}&queryParam3={queryParam3}"
http-method="POST"
request-factory="clientHttpRequestFactory"
header-mapper="headerMapper12345"
expected-response-type="java.lang.String">
<int-http:uri-variable name="queryParam1" expression="payload.getqueryParam1()"/>
<int-http:uri-variable name="queryParam2" expression="payload.getqueryParam2()"/>
<int-http:uri-variable name="queryParam3" expression="payload.getqueryParam3()"/>
</int-http:outbound-gateway>
</int:chain>
<bean id="headerMapper12345" class="org.springframework.integration.http.support.DefaultHttpHeaderMapper">
<property name="outboundHeaderNames" value="HTTP_REQUEST_HEADERS, header1,header2,header3" />
<property name="userDefinedHeaderPrefix" value="" /></bean>
</beans>
现在我的问题是如何传递多个标头(动态将在代码级别传递),多个查询参数(在代码级别动态)和请求正文(JSON 格式)以对其他服务进行休息调用。 我们可以从接口传递请求正文,但如何从接口或使用服务激活器传递标头和查询参数,然后相应地配置 xml 以对其他休息端点进行休息 http 出站网关调用。 提前致谢
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题不清楚,需要一些细节来揭示您到底在做什么以及您的期望是什么。
默认情况下,请求正文是从消息
payload
映射的。底层的 RestTemplate 将使用其 HttpMessageConverter 将此类正文序列化为网络上的相应数据表示形式。
标头根据以下规则从消息映射到 HTTP 请求:
https://docs.spring.io/spring-integration/docs/current/reference/html/http.html#http-header-mapping
所以,如果你有一些发送到此 HTTP 通道之前的标头集适配器,您可以在
mapped-request-headers
中指定它们的名称。或者只使用*
映射所有标头。可以使用
ref
和method
配置
来填充标头的动态映射:https://docs.spring.io/spring-integration/docs/current/reference/html/message-transformation.html#header-enricher
The question is not clear and requires some details to reveal what exactly are you doing and what are your expectations.
The request body is mapped from the message
payload
by default.The underlying
RestTemplate
is going to use itsHttpMessageConverter
s to serialize such a body to respective data presentation over the network.The headers are mapped from the message to the HTTP request according these rules:
https://docs.spring.io/spring-integration/docs/current/reference/html/http.html#http-header-mapping
So, if you have some set of headers before sending to this HTTP channel adapter, you can specify their names in the
mapped-request-headers
. Or just use*
to map all the headers.The
<header-enricher>
can be configured with theref
andmethod
to populate something dynamic map of headers:https://docs.spring.io/spring-integration/docs/current/reference/html/message-transformation.html#header-enricher