我有以下流程,接受来自http的JSON对象,然后 将JSON对象转换为标准Object,然后执行 对象的选择。
以下是流程:
<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns:tracking="http://www.mulesoft.org/schema/mule/ee/tracking" 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
http://www.mulesoft.org/schema/mule/ee/tracking http://www.mulesoft.org/schema/mule/ee/tracking/current/mule-tracking-ee.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"/>
<json:json-to-object-transformer doc:name="JSON to Object"/>
<choice doc:name="Choice-Determine-Route" tracking:enable-default-events="true">
<when expression="#[payload.uid == 'ABC']">
<set-payload value="#['When ABC case']" doc:name="Set Payload"/>
</when>
<otherwise>
<set-payload value="#['Default case, payload: ' + payload + ', payload.uid: ' + payload.uid]" doc:name="Set Payload"/>
</otherwise>
</choice>
</flow>
</mule>
以下是流量测试
C:\curl>type input2.txt
{ "uid" : "ABC" }
C:\curl>curl -H "Content-Type: application/json" -i -d @input2.txt http://localh
ost:8081
Content-Type: application/json
Default case, payload: {"uid":"ABC"}, payload.uid: null
即使我的input2.txt包含:
{ "uid" : "ABC" }
我无法获得以下表达式来成功触发
#[payload.uid == 'ABC']
现在注意Set Payload表达式
#['Default case, payload: ' + payload + ', payload.uid: ' + payload.uid]
......以及它的报道:
Default case, payload: {"uid":"ABC"}, payload.uid: null
因此它似乎无法识别payload.uid
我应该如何在MEL中引用它?
由于
答案 0 :(得分:2)
因为您尚未定义returnClass
属性,所以默认情况下会返回org.mule.module.json.JsonData的实例 - http://www.mulesoft.org/docs/site/current3/apidocs/org/mule/module/json/JsonData.html
所以在这种情况下你需要使用:
when expression="#[payload.get('UID') == 'ABC']"
如果您将变换器设置为返回带有uid字段的地图或对象,则可以使用。你试过的符号。
所以要使你的表达工作:
<json:json-to-object-transformer returnClass="java.util.HashMap" doc:name="JSON to Object"/>