Spring Integration DSL http Outboundgateway [英] spring integration dsl http outboundgateway
问题描述
我们正尝试使用DSL使用HTTP OutboundGateway调用REST API。我们可以使用GET和POST进行呼叫,并获得预期的响应。但是,我们无法找到一种使用DSL进行此调用时传递http标头的方法。关于XML方法的文章很多,但是找不到DSL的文档
We are trying to call a REST API using HTTP OutboundGateway using DSL. We are able to make the call using both GET and POST and getting the response as expected. However we couldnt figure a way to pass http headers while making this call using DSL. There are quite a lot of articles about XML approach but couldnt find out documentation with DSL
return IntegrationFlows.from("FileContentChannel")
.handle(Http.outboundGateway("http://host:port/paymentinfo/")
.charset("UTF-8")
.httpMethod(HttpMethod.GET)
.headerMapper(headers)
.expectedResponseType(String.class))
.channel(MessageChannels.queue("APIResponseChannel"))
.get();
我们也尝试了DefaultHttpHeaderMapper,但是没有用。
We tried with DefaultHttpHeaderMapper as well but it didnt work. Can you please guide us on this?
感谢Gary..it与
Update1
Thanks to Gary..it worked with this Update1
return IntegrationFlows.from("FileContentChannel")
.handle(Http.outboundGateway("http://host:port/paymentinfo/")
.charset("UTF-8")
.httpMethod(HttpMethod.GET)
.mappedRequestHeaders("pay*")
.headerMapper(headerMapper())
.expectedResponseType(String.class))
.channel(MessageChannels.queue("APIResponseChannel"))
.get();
@Bean
HeaderMapper headerMapper() {
DefaultHttpHeaderMapper headerMapper = new DefaultHttpHeaderMapper();
String[] headerNames = {"payment-hdr1","payment-hdr2"};
headerMapper.setOutboundHeaderNames(headerNames);
headerMapper.setUserDefinedHeaderPrefix("");
return headerMapper;
}
推荐答案
return IntegrationFlows.from("FileContentChannel")
.enrichHeaders(h -> h.header("foo1", "bar")
.header("foo2", "baz"))
.handle(Http.outboundGateway("http://host:port/paymentinfo/")
.charset("UTF-8")
.httpMethod(HttpMethod.GET)
.mappedRequestHeaders("foo*")
.expectedResponseType(String.class))
.channel(MessageChannels.queue("APIResponseChannel"))
.get();
自定义标题将(当前)获得 X-
前缀。
Custom headers will (currently) get an X-
prefix.
为避免这种情况,请使用自定义的 DefaultHeaderMapper
映射所需的标头模式并使用<$
中的c $ c> userDefinedHeaderPrefix 。
To avoid that use a customized DefaultHeaderMapper
to map the required header patterns and use a userDefinedHeaderPrefix
of ""
.
这篇关于Spring Integration DSL http Outboundgateway的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!