jsonmuleesbjsonpathmule-el

Mule ESB can not trigger Choice 'when' condition


I can not get Mule ESB to trigger a Choice 'when' condition

I converted a Hello World flow to to accept aJSON object (inserting a JSON to Object node) and execute a Choice node.

But I can not trigger the 'when' condition of the CHoice statement; I always goto the DEFAULT case. 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 == &quot;\042ABC\042&quot;]">
                <set-payload value="#['When ABC case']" doc:name="Set Payload"/>
            </when>
            <otherwise>
                <set-payload value="#['Default case, payload:  ' + payload + ', payload.get(\047uid\047): ' + payload.get('uid')]" doc:name="Set Payload"/>
            </otherwise>
        </choice>
    </flow>
</mule>

In this flow I use the following conditional and SetPayload (Default case) statements:

#[payload.uid == "\042ABC\042"]

    ( Note:  I used the \042 (double quote) because JSONPATH expressions will return "ABC" instead of ABC )

#['Default case, payload:  ' + payload + ', payload.get(\047uid\047): ' + payload.get('uid')]

Below is my output:

C:\curl>type input2.txt
{ "uid" : "ABC" }

C:\curl>curl -H "Content-Type: application/json" -i -d @input2.txt http://localh
ost:8081

Default case, payload:  {"uid":"ABC"}, payload.get('uid'): "ABC"

As you can see I did not trigger the 'when' clause. However, the SetPayload does output payload.get('uid') as "ABC"

So I try again three different times, with three different 'when' conditions:

#[payload.uid == "ABC"]

#[payload.get('uid') == "ABC"]

#[payload.get('uid') == "\042ABC\042"]

But in all three of these cases, I get the following:

Default case, payload:  {"uid":"ABC"}, payload.get('uid'): "ABC"

How can I resolve this ?

Thanks


Solution

  • Try this :-

    Updated:-

    <json:json-to-object-transformer returnClass="java.util.HashMap"  doc:name="JSON to Object" />
    
        <choice doc:name="Choice-Determine-Route" tracking:enable-default-events="true">
                     <when expression="#[message.payload.uid == 'ABC']">
                        <set-payload value="When ABC case" doc:name="Set Payload"/>
                    </when>
                    <otherwise>
                        <set-payload value="#['Default case, payload:  ' + payload + ', payload.get(\047uid\047): ' + payload.get('uid')]" doc:name="Set Payload"/>
                    </otherwise>
                </choice>