【问题标题】:extract query parameters from endpoint and append to SQL Query从端点提取查询参数并附加到 SQL 查询
【发布时间】:2018-03-12 05:44:12
【问题描述】:

我有一个这样的端点 api.com/users?id=id1,id2,id3,id4,id5...id500

  1. 我需要从端点提取查询参数,即 id1,id2 等等……
  2. 然后我需要将参数附加到这样的 SQL 语句中

    选择第 1 列,第 2 列 来自 my_table 其中 column3 像 'id1%'; 或 column3 之类的 'id2%' 或 column3 之类的 'id4%' ..

.

关于如何完成此任务的任何想法? 我对 Mulesoft 很陌生。

#[message.inboundProperties.'http.query.params'.size()] gives me the number of values in the parameters

#[message.inboundProperties.'http.query.params'.id] perhaps would give me an collection or arrayList ?

我想循环遍历 arrayList 的每个项目,并从类似的字符串中循环 where column3 like arrayList[0] + '%'

但我不知道如何实现这一点并将其附加到我分派到数据库的 SQL 查询中。

【问题讨论】:

    标签: mule


    【解决方案1】:

    1.获取查询字符串的值并设置payload。

    <set-payload value="id1,id2,id3,id4,id5" doc:name="Set Sample Payload"/>
    

    2.创建字符串缓冲区

      <expression-component doc:name="Expression"><![CDATA[StringBuffer sb = new 
        StringBuffer();
        flowVars.stBuffer=sb;
        ]]></expression-component>
    

    3.拆分payload

     <expression-transformer expression="#[message.payload.split(&quot;,&quot;)]" 
        doc:name="Split String"/>
    

    4.通过在字符串缓冲区中追加值,在for循环中构造sql

    <foreach collection="#[payload]" doc:name="For Each">
               Construct sql here using string buffer
       </foreach>
    

    参考:Splitting a comma-separated string and saving to database without DataWeave in Mule

    【讨论】:

      【解决方案2】:

      您可以将查询参数设置为 application/json 并以 Json 格式传递其中的单个值,其中包含可以在 API 中轻松提取的字段和值。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-07-23
        • 1970-01-01
        • 2023-03-28
        • 2014-11-30
        • 2023-02-25
        • 1970-01-01
        • 1970-01-01
        • 2019-11-29
        相关资源
        最近更新 更多