如何在http:outbound-gateway中记录请求数据和响应数据

时间:2017-11-15 20:40:15

标签: spring-boot spring-integration

使用spring集成,我从队列中读取,然后使用http:outbound-gateway调用REST服务。代码工作正常。但我希望将日志关联起来,以便对于给定的请求密钥字段,接收响应。否则,当有数千条消息流过队列时,很难确定收到响应的请求。

我的服务调用JSON请求如下: {" ID":" 123""状态":" A"}

我的JSON来自服务调用的响应如下: {"的TransactionStatus":"成功"}

我想记录" ID:123"有一个响应transactionStatus作为"成功"。 请帮我解释一下如何实现这一目标。如果您需要更多详细信息,请与我们联系。 提前谢谢。

<int:object-to-json-transformer input-channel = "gcmRequestChannel" output-channel="RESTSrvcChannel"></int:object-to-json-transformer>

<int:header-enricher id = "restenricher" input-channel = "RESTSrvcChannel" output-channel = "RESTSrvcChannel2">
    <int:header name="contentType" value="application/json"/>
    <int:header name="SPApikey" value="${throttler.SPApikey}" />
</int:header-enricher>
 <http:outbound-gateway id="gcmrestHttpOutboundGateway" request-channel="RESTSrvcChannel2" reply-channel="nullChannel"
                       extract-request-payload="true"
                       url="${throttler.url}"
                       header-mapper="headerMapper"
                       http-method="POST"
                       expected-response-type="java.lang.String"
                       >

</http:outbound-gateway>
<beans:bean id="headerMapper"
    class="org.springframework.integration.http.support.DefaultHttpHeaderMapper">
    <beans:property name="inboundHeaderNames" value="*" />
    <beans:property name="outboundHeaderNames" value="HTTP_REQUEST_HEADERS,SPApikey" />
    <beans:property name="userDefinedHeaderPrefix" value="" />
</beans:bean>

2 个答案:

答案 0 :(得分:1)

由于Spring Integration处理其组件中的Message对象以及它们之间的通道,因此有一个很好的解决方案,例如将重要信息提供到标题中,它们将在replyMessage中提供你的记录目的:

<header-enricher>
    <header name="originalPayload" expression="payload"/>
</header-enricher>

答案 1 :(得分:1)

我按照您的建议编码。谢谢你的回复。

<int:header-enricher id = "gcmrestenricher" input-channel = "gcmRESTSrvcChannel" output-channel = "gcmRESTSrvcChannel2">
    <int:header name="contentType" value="application/json"/>
    <int:header name="SPApikey" value="${throttler.SPApikey}" />
    <int:header name="JSONPayload" expression="payload"/>
</int:header-enricher>

<http:outbound-gateway id="gcmrestHttpOutboundGateway" request-channel="gcmRESTSrvcChannel2" reply-channel="gcmRESTSrvcOutputChannel"
                       extract-request-payload="true"
                       url="${throttler.gcmurl}"
                       header-mapper="gcmheaderMapper"
                       http-method="POST"
                       expected-response-type="java.lang.String"
                       >

</http:outbound-gateway>
<beans:bean id="gcmheaderMapper"
    class="org.springframework.integration.http.support.DefaultHttpHeaderMapper">
    <beans:property name="inboundHeaderNames" value="*" />
    <beans:property name="outboundHeaderNames" value="HTTP_REQUEST_HEADERS,SPApikey,JSONPayload" />
    <beans:property name="userDefinedHeaderPrefix" value="" />
</beans:bean>
<beans:bean id="logmsg" class = "com.MLAServiceActivator"></beans:bean>
<int:service-activator requires-reply="false" input-channel="gcmRESTSrvcOutputChannel" ref="logmsg" method="ResponseLogging"></int:service-activator>


@ServiceActivator
public void ResponseLogging(Message<String> message)
{
    logger.info("The response message is:"+message.getPayload()+ " for request message:"+message.getHeaders().get("JSONPayload"));
}