简体   繁体   中英

Mule ESB: Can not get MEL to access field

I have the following flow, that accepts a JSON object from http, then converts the JSON object to a standard Object, and then performs a CHOICE on the object.

Below is the flow:

<?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>

Below is the test of the flow

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

So even though my input2.txt contained:

{ "uid" : "ABC" }

I could not get the following expression to trigger successfully

#[payload.uid == 'ABC']

Now notice the Set Payload expression

#['Default case, payload:  ' + payload + ', payload.uid: ' + payload.uid]

... and what it reported:

Default case, payload:  {"uid":"ABC"}, payload.uid: null

So it does not seem to recognize payload.uid

How should I reference this in MEL ?

Thanks

Because you haven't defined the returnClass attribute,it will by default return an instance of org.mule.module.json.JsonData - http://www.mulesoft.org/docs/site/current3/apidocs/org/mule/module/json/JsonData.html

So in that case you need to use:

when expression="#[payload.get('UID') == 'ABC']"

If instead you set the transformer to return a map or an object with a uid field you can navigate it using the . notation you tried.

So to make your expression work use:

<json:json-to-object-transformer returnClass="java.util.HashMap" doc:name="JSON to Object"/>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM