spring - 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 端点。
解决方案
可以使用适当的属性将自定义标头映射到 HTTP 标头 - mapped-request-headers
.
对于 JSON 请求,如果您有一个on classpath ,<int-http:outbound-gateway>
则提供配置了一个。您的应用程序只需要发送一个可以序列化为 JSON 的 POJO,而重要的是 -带有值的标头。RestTemplate
MappingJackson2HttpMessageConverter
jackson-databind
MessageHeaders.CONTENT_TYPE
application/json
推荐阅读
- sparql - Wikidata:仅过滤当前有效的邮政编码
- android - 如何在 MacBook M1 上的 Android 模拟器中为 API 30 和 29 获取 ARM 64?
- opencascade - 如何找到构成 stp 文件面的顶点和三角形
- authentication - ASP.Net Core MVC IdentityFrameWork 不工作
- javascript - 如何通过创建反应应用启用 JIT(即时模式)?
- javascript - 如何在数据表中使用输入字段添加最小和最大范围过滤器?
- powershell - 如何在 PowerShell 中执行 curl 命令以从 JFrog Artifactory 下载文件而不调用 cmd.exe?
- javascript - Javascript倒计时以获取当前间隔时间运行
- node.js - TDengine数据库nodejs连接器毫秒精度
- python - 为什么我添加的特殊标记没有在掩码语言建模中显示为预测?