【问题标题】:Spring integration dsl outboundGateway set query parameter using expressionSpring集成dsl outboundGateway使用表达式设置查询参数
【发布时间】:2018-05-02 11:10:08
【问题描述】:

我在 Spring 集成流程中有一个休息请求。如何从消息中的payload或header中选择值并设置url的查询参数

.handle(Http.outboundGateway(UriComponentsBuilder.fromHttpUrl("localhost:8080).path("search").queryParam("order", "orderId").toUriString())
.httpMethod(HttpMethod.GET)
.expectedResponseType(Order.class))

在上面的代码中,我需要从payload中获取orderId

【问题讨论】:

    标签: spring-integration spring-integration-dsl


    【解决方案1】:

    如果你坚持使用UriComponentsBuilder,那么是这样的:

    .handle(Http.outboundGateway(m ->
            UriComponentsBuilder.fromHttpUrl("localhost:8080")
                        .path("search")
                        .queryParam("order", m.getPayload())
                        .build())
                .httpMethod(HttpMethod.GET)
                .expectedResponseType(Order.class))
    

    或者像这样:

    .handle(Http.outboundGateway(m -> servieUrl + "/search?orderId={orderId}")
                .uriVariable("orderId", m -> m.getPayload())
                .httpMethod(HttpMethod.GET)
                .expectedResponseType(Order.class))
    

    【讨论】:

    • 上述解决方案有效,是否有任何巧妙的方法以不同的方式做到这一点
    • 不要使用UriComponentsBuilder,而只是简单的String 用于uri,并且Http.outboundGateway() 构建器中有uriVariable() 以使用变量增强目标uri。我认为我们需要改进参考手册中的 HTTP 章节以包含 Java DSL 示例。随意就此事提出 JIRA
    • 如果我使用了 uriVariable,那么应该写完整的 url,看起来像 Http.outboundGateway("localhost:8080/search?orderId={orderId}").uriVariable("orderId", m -> m.getPayload())
    • 正确。不好吗?
    • "localhost:8080" 来自属性文件,并且在多个地方使用过,在构造像 servieUrl + "/search?orderId={orderId}" 这样的 url 时我应该小心,否则看起来很酷
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-30
    • 1970-01-01
    • 2016-06-21
    • 2020-11-06
    • 2020-04-21
    • 2014-04-22
    相关资源
    最近更新 更多