【问题标题】:Path parameter in URL for http outbound gatewayhttp 出站网关的 URL 中的路径参数
【发布时间】:2020-06-02 12:25:27
【问题描述】:

我有一个包含这个的 xml:

<channel id="myDataChannel"></channel>
<http:outbound-gateway 
    request-channel="myDataChannel"
    url="{url}/myApi/getData/{id}"
    expected-response-type="com.api.dto.Data"
    http-method="GET" 
    rest-template="myRestTemplate">
    <http:uri-variable name="url" expression="headers.url" />
</http:outbound-gateway>

在java代码中是这样的:

    MessagingTemplate myTemplate = new MessagingTemplate();
    Message<?> getDataReply = null;
    Data dataDto = null;

    Message<?> requestMsg = MessageBuilder.withPayload(requestDto)
            .build();

    getDataReply = template.sendAndReceive(myDataChannel, requestMsg);
    return dataDto = (Data) getDataReply.getPayload();

这是我的问题,如何使用 java 代码中的有效负载在 URL 中路径 id,我应该在 out-bound-gateway 中向 XML 添加一个额外的标签吗?

【问题讨论】:

    标签: java spring spring-integration spring-integration-http


    【解决方案1】:

    您的问题不清楚,但您需要类似的东西

    Message<?> requestMsg = MessageBuilder.withPayload(requestDto)
            .setHeader("url", ...)
            .setHeader("id", ...)
            .build();
    

    <http:uri-variable name="id" expression="headers.id" />
    

    或者,如果 id 是有效负载的属性

    Message<?> requestMsg = MessageBuilder.withPayload(requestDto)
            .setHeader("url", ...)
            .build();
    

    <http:uri-variable name="id" expression="payload.id" />
    

    【讨论】:

    • 我的意思是,例如,当我调用 api 时,我有来自请求的 id 并希望像这样的 URL 发送它的值:HTTP//localhost:8080/myApi/getData/2 如果我应用你的解决方案,它会像我评论中的 URL 一样创建它吗?
    • 是的,您只需告诉适配器在哪里可以找到id
    猜你喜欢
    • 1970-01-01
    • 2015-03-28
    • 1970-01-01
    • 1970-01-01
    • 2017-03-19
    • 2017-02-03
    • 2013-06-12
    • 1970-01-01
    • 2019-01-05
    相关资源
    最近更新 更多