spring integration dsl http outboundgateway

£可爱£侵袭症+ 提交于 2020-01-24 19:43:04

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!