Spring集成:在http出站网关中将json作为请求体和头发送
我希望使用JSON负载调用外部REST端点POST请求,该负载将通过http入站网关从另一个服务调用 我正在为我的应用程序使用以下配置:Spring集成:在http出站网关中将json作为请求体和头发送,spring,spring-integration,Spring,Spring Integration,我希望使用JSON负载调用外部REST端点POST请求,该负载将通过http入站网关从另一个服务调用 我正在为我的应用程序使用以下配置: <int:channel id="xappSearchRequest" /> <int:channel id="xappSearchResponse" /> <int:channel id="xappFilterChannelOutput"/> <int:channel id="discardFilterChanne
<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头 关于此事有完整的文件: 对于JSON,请求随RestTemplate一起提供,如果类路径上有jackson数据绑定,则RestTemplate配置了MappingJackson2HttpMessageConverter。应用程序只需要发送一个可以序列化为JSON的POJO,这一点很重要——一个带有application/JSON值的MessageHeaders.CONTENT\u类型头。解决了这个问题。 我使用的出站网关如下所示:
<int-http:outbound-gateway id="get.outbound.gateway"
request-channel="postCmsChannel" url="${cms.stub.request.url}"
http-method="POST" reply-channel="zappSearchResponse"
expected-response-type="standalone.StubResponseBody">
</int-http:outbound-gateway>
@Autowired
@Qualifier("postCmsChannel")
MessageChannel postCmsChannel;
public void handleXappRequest(Message<CFIRequestBody> inMessage){
/*
* Map<String, Object> responseHeaderMap = new HashMap<String, Object>();
*
* //MessageHeaders headers = inMessage.getHeaders(); StubResponseBody
* info=inMessage.getPayload();
*
* setReturnStatusAndMessage(responseHeaderMap); Message<StubResponseBody>
* message = new GenericMessage<StubResponseBody>(info, responseHeaderMap);
* return message;
*/
inMessage.getHeaders();
Map<String,String> headerMap=new HashMap<String,String>();
headerMap.put("X-JWS-SIGNATURE", "dshdgshdgasshgdywtwtqsabh232wgd7wdyt");
headerMap.put("X-PARTICIPANT-ID", "CMDRV2112BB");
postCmsChannel.send(MessageBuilder.withPayload(inMessage.getPayload())
.copyHeadersIfAbsent(inMessage.getHeaders()).copyHeadersIfAbsent(headerMap).build());
}
}
“PostMSCChannel”是service activator的输出通道:
<int:service-activator id="zappServiceActivator"
input-channel="xappFilterChannelOutput"
output-channel="postCmsChannel"
ref="xappSearchService"
method="handleXappRequest"
send-timeout="60000"/>
“xappSearchService”如下所示:
<int-http:outbound-gateway id="get.outbound.gateway"
request-channel="postCmsChannel" url="${cms.stub.request.url}"
http-method="POST" reply-channel="zappSearchResponse"
expected-response-type="standalone.StubResponseBody">
</int-http:outbound-gateway>
@Autowired
@Qualifier("postCmsChannel")
MessageChannel postCmsChannel;
public void handleXappRequest(Message<CFIRequestBody> inMessage){
/*
* Map<String, Object> responseHeaderMap = new HashMap<String, Object>();
*
* //MessageHeaders headers = inMessage.getHeaders(); StubResponseBody
* info=inMessage.getPayload();
*
* setReturnStatusAndMessage(responseHeaderMap); Message<StubResponseBody>
* message = new GenericMessage<StubResponseBody>(info, responseHeaderMap);
* return message;
*/
inMessage.getHeaders();
Map<String,String> headerMap=new HashMap<String,String>();
headerMap.put("X-JWS-SIGNATURE", "dshdgshdgasshgdywtwtqsabh232wgd7wdyt");
headerMap.put("X-PARTICIPANT-ID", "CMDRV2112BB");
postCmsChannel.send(MessageBuilder.withPayload(inMessage.getPayload())
.copyHeadersIfAbsent(inMessage.getHeaders()).copyHeadersIfAbsent(headerMap).build());
}
}
你能给我一个例子,在我不确定你需要什么的情况下,JSON请求作为有效负载发送。我相信您有一些POJO,它实际上可以作为HTTP网关的消息负载。您只需要在发送到该网关之前添加ContentType=application/json头。我已经编辑了我的原始帖子,包含了完整的配置和要求。我的集成层位于两个REST服务之间……您没有显示如何发送以获取发送通道,请注意,这实际上就是我被卡住的地方…xappServiceActivator必须调用出站网关,然后将输出返回到实际调用的服务