如何在spring集成中传递多个动态头和一个请求体来进行rest调用



MyGateway(在服务接口中定义(是一个接口,它将包含一个请求体(JsonFormat(,我将在http出站网关中传递该请求体以进行rest调用

<!-- 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,我创建了一个模型类,它将包含标头的值,这样我就可以在执行rest调用时使用动态标头--->低于

<int:service-activator input-channel="requestChannel12345"
output-channel="responseChannel1234567" 
ref="methodInsideService" 
method="methodExample"/>
<bean id="clientHttpRequestFactory" class="org.springframework.http.client.HttpComponentsClientHttpRequestFactory"/>

此处我接受请求通道(responseChannel1234567(作为动态标头响应的输出通道(responseCohannel1234567--->在这里,我的实际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}&amp;queryParam2={queryParam2}&amp;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格式(来对其他服务进行rest调用。我们可以从接口传递请求体,但如何从接口或使用服务激活器传递头和查询参数,然后相应地配置xml,以便对其他rest端点进行rest http出站网关调用。提前感谢

这个问题并不清楚,需要一些细节来揭示你到底在做什么以及你的期望是什么。

默认情况下,请求主体从消息payload映射而来。底层RestTemplate将使用其HttpMessageConverter来将这样的主体串行化为网络上的相应数据表示。

标头根据以下规则从消息映射到HTTP请求:

https://docs.spring.io/spring-integration/docs/current/reference/html/http.html#http-标头映射

因此,如果在发送到此HTTP通道适配器之前有一组标头,则可以在mapped-request-headers中指定它们的名称。或者只使用*来映射所有标头。

<header-enricher>可以配置有refmethod,以填充标头的动态映射:

<xsd:attribute name="ref" type="xsd:string">
<xsd:annotation>
<xsd:documentation>
Reference to an Object to be invoked for header values.
The 'method' attribute is required
along
with this.
</xsd:documentation>
<xsd:appinfo>
<tool:annotation kind="ref"/>
</xsd:appinfo>
</xsd:annotation>
</xsd:attribute>
<xsd:attribute name="method" type="xsd:string">
<xsd:annotation>
<xsd:documentation>
Method to be invoked on the referenced Object as specified by the
'ref' attribute. The method
should return a Map with String-typed keys.
</xsd:documentation>
<xsd:appinfo>
<tool:annotation>
<tool:expected-method type-ref="@ref"/>
</tool:annotation>
</xsd:appinfo>
</xsd:annotation>
</xsd:attribute>

https://docs.spring.io/spring-integration/docs/current/reference/html/message-transformation.html#header-浓缩

最新更新