首页 > 解决方案 > Spring 集成:在 http 出站网关中将 json 作为请求正文和标头发送

问题描述

我想使用 JSON 有效负载调用外部 REST 端点 POST 请求,该有效负载将通过 http 入站网关从另一个服务调用。

我正在为我的应用程序使用以下配置:

<int:channel id="xappSearchRequest" />
<int:channel id="xappSearchResponse" />

<int:channel id="xappFilterChannelOutput"/>
<int:channel id="discardFilterChannel"/>
<int:channel id="mutableMessageChannel"/>

<int:filter  input-channel="mutableMessageChannel" output-channel="xappFilterChannelOutput" discard-channel="discardFilterChannel" ref="structureValidationFilter"/>

 <int:transformer input-channel="xappSearchRequest" output-channel="mutableMessageChannel"
    ref="mutableMessageTransformer" />


<int-http:inbound-gateway id="inboundxappSearchRequestGateway"
    supported-methods="POST"
    request-channel="xappSearchRequest"
    reply-channel="xappSearchResponse"
    mapped-response-headers="Return-Status, Return-Status-Msg, HTTP_RESPONSE_HEADERS"
    path="${xapp.request.path}"
    reply-timeout="50000"
    request-payload-type="standalone.CFIRequestBody">
</int-http:inbound-gateway>

<int:service-activator id="xappServiceActivator"
                input-channel="xappFilterChannelOutput"
                output-channel="xappSearchResponse"
                ref="xappSearchService"
                method="handlexappRequest"
                requires-reply="true"
                send-timeout="60000"/>



<int:service-activator id="dicardPayloadServiceActivator"
                input-channel="discardFilterChannel"
                output-channel="xappSearchResponse"
                ref="invalidPayloadService"
                method="getInvalidMessage"
                requires-reply="true"
                send-timeout="60000"/>


<int-http:outbound-gateway id="get.outbound.gateway"
    request-channel="get_send_channel" url="${cms.stub.request.url}"
    http-method="POST" reply-channel="get_receive_channel"
    expected-response-type="standalone.StubResponseBody">
</int-http:outbound-gateway>

无法弄清楚如何发送 JSON 有效负载和自定义标头来调用 POST 端点。

标签: springspring-integration

解决方案


可以使用适当的属性将自定义标头映射到 HTTP 标头 - mapped-request-headers.

有关于此事的完整文档:https ://docs.spring.io/spring-integration/docs/5.2.3.RELEASE/reference/html/http.html#http-header-mapping

对于 JSON 请求,如果您有一个on classpath ,<int-http:outbound-gateway>则提供配置了一个。您的应用程序只需要发送一个可以序列化为 JSON 的 POJO,而重要的是 -带有值的标头。RestTemplateMappingJackson2HttpMessageConverterjackson-databindMessageHeaders.CONTENT_TYPEapplication/json


推荐阅读