【问题标题】:Compare JSONPath value with flowVars value in Mule ESB将 JSONPath 值与 Mule ESB 中的 flowVars 值进行比较
【发布时间】:2017-08-09 06:09:38
【问题描述】:

我需要将下面 JSON 中的用户名与 Mule ESB 3.8.3 中的 flowVars 进行比较

{"id":"users_0001","username":"0001","firstName":"AB","lastName":"C","email":"abc@abc.com","enabled":true}

在选择运算符中使用此表达式

<choice doc:name="Choice">
    <when expression="#[json:[0]/username != flowVars.username]">
        <flow-ref name="put account" doc:name="put account"/>
    </when>
    <otherwise>
        <flow-ref name="do nothing" doc:name="do nothing"/>
    </otherwise>
 </choice>

在调试过程中,我可以看到 json:[0]/username 和 flowVars.username 都返回相同的值,但是为什么在比较它们时总是返回 false?

这是我评估它们时的结果

flowVars.username == "0001", returns true
flowVars.username == '0001', returns true
flowVars.username == 0001, returns true
json:[0]/username = 0001, returns true
json:[0]/username = "0001", returns false
json:[0]/username = '0001', returns false
json:[0]/username != flowVars.username, returns true
json:[0]/username = flowVars.username, returns false

【问题讨论】:

    标签: json mule string-comparison jsonpath mule-esb


    【解决方案1】:

    我会避免使用 #[json] 工具,而只是将传入的 JSON 转换为 HashMap。您的生活会更轻松,而且使用 HashMap 更容易。

    根据文档:https://docs.mulesoft.com/mule-user-guide/v/3.7/mule-expression-language-tips

    JSON 处理 MEL 不直接支持 JSON。 json-to-object-transformer 可以将 JSON 有效负载转换为简单数据结构的层次结构,这些结构易于使用 MEL 进行解析。

    在你的情况下,是这样的:

        <json:json-to-object-transformer returnClass="java.util.HashMap" doc:name="JSON to Object"/>
        <choice doc:name="Choice">
            <when expression="#[payload.username != flowVars.username]">
                <flow-ref name="put account" doc:name="put account"/>
            </when>
            <otherwise>
                 <flow-ref name="do nothing" doc:name="do nothing"/>
            </otherwise>
        </choice>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-04-24
      • 2011-10-24
      • 2022-12-12
      • 1970-01-01
      • 2011-02-08
      • 1970-01-01
      • 2019-12-27
      相关资源
      最近更新 更多