【问题标题】:Spring Integration : send json as request body and headers in http outbound gatewaySpring 集成:在 http 出站网关中将 json 作为请求正文和标头发送
【发布时间】:2020-02-17 13:49:38
【问题描述】:

我想调用一个带有 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 端点。

【问题讨论】:

    标签: spring spring-integration


    【解决方案1】:

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

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

    对于 JSON 请求,&lt;int-http:outbound-gateway&gt; 与 RestTemplate 一起提供,如果您在类路径上有 jackson-databind,则配置 MappingJackson2HttpMessageConverter。您的应用程序只需要发送一个可以序列化为 JSON 的 POJO,重要的是 - 带有 application/json 值的 MessageHeaders.CONTENT_TYPE 标头。

    【讨论】:

    • 你能给我举个例子,在 中将 JSON 请求作为有效负载发送
    • 我不确定您需要什么。我相信您有一些 POJO,它确实可以作为消息的有效负载发送到该 HTTP 网关。您只需要在发送到该网关之前添加一个contenType="application/json" 标头。
    • 我已经用完整的配置和要求编辑了我的原始帖子。我的集成层位于两个 REST 服务之间......
    • 请不要显示发送到get_send_channel的方式
    • 这实际上是我卡住的地方...xappServiceActivator 必须调用出站网关,然后将输出返回给实际调用服务
    【解决方案2】:

    想通了。 我正在使用如下的出站网关:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-28
      • 1970-01-01
      • 1970-01-01
      • 2014-06-08
      • 2014-05-10
      相关资源
      最近更新 更多