问题
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();
We tried with DefaultHttpHeaderMapper as well but it didnt work. Can you please guide us on this?
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;
}
回答1:
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();
Custom headers will (currently) get an X-
prefix.
To avoid that use a customized DefaultHeaderMapper
to map the required header patterns and use a userDefinedHeaderPrefix
of ""
.
来源:https://stackoverflow.com/questions/40248152/spring-integration-dsl-http-outboundgateway