【问题标题】:Mule ESB: How to use a flow execution conditional on a JSON objectMule ESB:如何在 JSON 对象上使用流执行条件
【发布时间】:2023-11-27 19:51:01
【问题描述】:

骡子 ESB

我正在尝试将 Hello World 程序转换为接受 JSON 对象、检查数据并相应地路由执行的程序。

以下是我的流程的副本:

<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns:json="http://www.mulesoft.org/schema/mule/json" version="EE-3.5.0" xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:core="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation" xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns:spring="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd
http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/json http://www.mulesoft.org/schema/mule/json/current/mule-json.xsd">
    <json:object-to-json-transformer name="Object_to_JSON" doc:name="Object to JSON"/>
    <flow doc:name="HelloWorldFlow1" name="HelloWorldFlow1">
        <http:inbound-endpoint doc:description="This endpoint receives an HTTP message." doc:name="HTTP" exchange-pattern="request-response" host="localhost" port="8081" contentType="application/json"/>
        <echo-component doc:name="Echo"/>
    </flow>
</mule>

我在代码中看到了 object-to-json,我相信它是从 HTTP节点->HTTP设置->内容类型,设置为

application/json

所以我假设传入的对象在流中一次是 JSON 格式,不需要进一步的对象到 JSON 节点。

我将以下字符串发送到流:

{ "uid" : "ABCxxx" }

我想要获得一个节点,该节点将检查并验证 uid 的前三个字母是否为“ABC”,如果是,则将其发送到一条路径,但如果 uid 的前三个字符确实如此不等于“ABC”,走另一条路。有点像一个带有真假配置的IF语句,

以下是伪代码示例

IF uid[3] == "ABC"
   GOTO Database Connector
else
   GOTO JSON-TO-OBJECT Transformer

我的问题是:我应该使用哪个节点来执行此操作,我应该使用表达式过滤器还是其他

...以及如何在 JSAONPath(或其他)中编写它

(Mule ESB 会执行这种操作吗?)

【问题讨论】:

    标签: json conditional mule esb


    【解决方案1】:

    不推荐使用在 mule 中执行 JsonPath 的 json 评估器,而支持 MEL。现在首选的方法是将 json 转换为对象并改为查询该对象。最简单的方法之一是将 json 转换为地图并使用 MEL 查询地图。像这样:

            <json:json-to-object-transformer returnClass="java.util.HashMap" />
    
            <choice>
                <when expression="#[payload.uid == 'ABC']">
    
                </when>
            </choice>
    

    更多信息在这里:http://www.mulesoft.org/documentation/display/current/Mule+Expression+Language+Tips

    【讨论】:

      【解决方案2】:

      您也可以&lt;json:json-to-object-transformer returnClass="java.lang.Object" /&gt; 或者 然后

       <choice>
              <when expression="#[message.payload.uid == 'ABC']">
      

      【讨论】:

        最近更新 更多