Spring Integration: отправьте json как тело запроса и заголовки в исходящем http-шлюзе - PullRequest
0 голосов
/ 17 февраля 2020

Я хочу вызвать внешний POST-запрос конечной точки REST с полезной нагрузкой JSON, которая будет вызываться из другой службы через входящий шлюз 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.

Ответы [ 2 ]

1 голос
/ 17 февраля 2020

Пользовательские заголовки могут быть сопоставлены с заголовками HTTP с помощью соответствующего свойства - mapped-request-headers.

По этому вопросу имеется полная документация: https://docs.spring.io/spring-integration/docs/5.2.3.RELEASE/reference/html/http.html#http -header-mapping

Для запроса JSON <int-http:outbound-gateway> поставляется с RestTemplate, для которого настроен MappingJackson2HttpMessageConverter, если у вас есть jackson-databind на пути к классам. Единственное, что вам нужно из вашего приложения, - это отправить POJO, который можно сериализовать в JSON и, что важно, - заголовок MessageHeaders.CONTENT_TYPE со значением application/json.

0 голосов
/ 19 февраля 2020

понял это. Я использую исходящий шлюз, как показано ниже:

<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> 

'postCmsChannel' является выходным каналом активатора службы:

   <int:service-activator id="zappServiceActivator"
input-channel="xappFilterChannelOutput"
output-channel="postCmsChannel"
ref="xappSearchService"
method="handleXappRequest"
send-timeout="60000"/>

'xappSearchService' похож на ниже:

@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());

 }

}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...