I am using Anypoint Studio 6.1 and Mule 3.8.1 and want to return an Exception message with the "\n" returned as a new line. I am trying to use System.getProperty("line.separator") but the "\n" still appear in the message as text and not a new line.
How can I get this to work?
XML Test Flow:
<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
xmlns:spring="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="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/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd">
<flow name="tempflowFlow">
<http:listener config-ref="HTTP_Listener_Configuration" path="/test-flow" allowedMethods="GET" doc:name="HTTP"/>
<set-payload value="{
"Exception": {
"status": -1,
"description": "\nJson content is not compliant with schema\ncom.github.fge.jsonschema.core.report.ListProcessingReport: failure\n--- BEGIN MESSAGES ---\nerror: object has too many properties (found 2 but schema requires at most 1)\n level: \"error\"\n schema: {\"loadingURI\":\"file:/C:/temp.json#\",\"pointer\":\"/properties/field1\"}\n instance: {\"pointer\":\"/field1\"}\n domain: \"validation\"\n keyword: \"maxProperties\"\n found: 2\n required: 1\n--- END MESSAGES ---\n (org.mule.module.json.validation.JsonSchemaValidationException)."
}
}" encoding="UTF-8" mimeType="application/json" doc:name="Set Payload"/>
<logger message="#[payload.replace("\n", System.getProperty('line.separator'))]" level="INFO" doc:name="Logger"/>
<set-payload value="#[payload.replace("\n", System.getProperty('line.separator'))]" encoding="UTF-8" mimeType="application/json" doc:name="Set Payload"/>
</flow>
</mule>
Thanks
Looks like you need to escape the newline character. Also, use single quotes (') instead of double quotes (") inside MEL expression.
<logger message="#[payload.replace('\\n', System.getProperty('line.separator'))]" level="INFO"
doc:name="Logger" />
<set-payload value="#[payload.replace('\\n', System.getProperty('line.separator'))]" encoding="UTF-8"
mimeType="application/json" doc:name="Set Payload" />
Above code seems to work:
INFO 2017-02-09 18:03:09,349 [Mule.app.deployer.monitor.1.thread.1] org.mule.module.launcher.MuleDeploymentService:
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+ Started app 'test2' +
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
INFO 2017-02-09 18:03:17,280 [[test2].HTTP_Listener_Configuration.worker.01] org.mule.api.processor.LoggerMessageProcessor: { "Exception": { "status": -1, "description": "
Json content is not compliant with schema
com.github.fge.jsonschema.core.report.ListProcessingReport: failure
--- BEGIN MESSAGES ---
error: object has too many properties (found 2 but schema requires at most 1)
level: \"error\"
schema: {\"loadingURI\":\"file:/C:/temp.json#\",\"pointer\":\"/properties/field1\"}
instance: {\"pointer\":\"/field1\"}
domain: \"validation\"
keyword: \"maxProperties\"
found: 2
required: 1
--- END MESSAGES ---
(org.mule.module.json.validation.JsonSchemaValidationException)." } }
Mule log
Related
My Mulesoft POC contains globalErrorHandler as well as localErrorHandler having OnErrorContinue scope, I noticed yet it throws a MULE:EXPRESSION error ,Can anyone let me know the reason for it . The code is below enclosed with the pic of the flow .
Code
<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns:tracking="http://www.mulesoft.org/schema/mule/ee/tracking" xmlns:ee="http://www.mulesoft.org/schema/mule/ee/core"
xmlns:http="http://www.mulesoft.org/schema/mule/http"
xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
http://www.mulesoft.org/schema/mule/ee/core http://www.mulesoft.org/schema/mule/ee/core/current/mule-ee.xsd
http://www.mulesoft.org/schema/mule/ee/tracking http://www.mulesoft.org/schema/mule/ee/tracking/current/mule-tracking-ee.xsd">
<http:listener-config name="HTTP_Listener_config" doc:name="HTTP Listener config" doc:id="f8f3f4bb-341a-43ec-acf8-9ed33419f5ef" basePath="/error" >
<http:listener-connection host="0.0.0.0" port="8082" />
</http:listener-config>
<configuration doc:name="Configuration" doc:id="689a9085-287e-43e4-8e3d-5eeac857dc64" defaultErrorHandler-ref="globalError_Handler"/>
<flow name="example-errorFlow" doc:id="c37abd41-2143-4da1-848a-a8c240853b60" >
<http:listener doc:name="Listener" doc:id="9cda3f2d-a48d-40bd-96a4-54d856d0438e" config-ref="HTTP_Listener_config" path="/"/>
<set-payload value='"MAIN"' doc:name="Set Payload" doc:id="a1b3890f-6157-4716-84d3-8f6ccd6e9af5" />
<ee:transform doc:name="Transform Message" doc:id="9fc2571b-7038-4359-82db-be624d48b343" >
<ee:message >
<ee:set-payload ><![CDATA[%dw 2.0
output application/json
---
["RESPONSE"] ++ payload ]]></ee:set-payload>
</ee:message>
</ee:transform>
<error-handler >
<on-error-continue enableNotifications="true" logException="true" doc:name="On Error Continue" type= "HTTP:NOT_FOUND" doc:id="e6f2b028-e3ca-4eff-b878-0f41dd53d693" >
<set-payload value='"HTTP:NOT_FOUND"' doc:name='"HTTP:NOT_FOUND"' doc:id="c465edf3-bbac-4fca-bfb1-f2a6678e1173" />
</on-error-continue>
</error-handler>
</flow>
<error-handler name="globalError_Handler" doc:id="05e153b7-d663-4aa1-85c0-8fefbf76c39b" >
<on-error-continue enableNotifications="true" logException="true" doc:name="On Error Continue" doc:id="c0e80820-db0f-4b3f-b0e2-c74c47e3a5e7" >
<set-payload value="global Error Handler" doc:name="global Error Handler" doc:id="0055b184-4dd6-4d20-8284-27534570f723" />
</on-error-continue>
</error-handler>
</mule>
FlowImage
The error is coming from your Transform component but you are handling only HTTP:NOT FOUND error.
Handle all exceptions as described here
https://simpleflatservice.com/mule4/OnErrorContinue.html
In particular - simple remove error type and you will see actual message and can fix it.
Post it here and we can help.
In the transform you're trying to use ++ function with an Array and String which is incorrect (in your case its ["RESPONSE"] ++ payload where payload = "MAIN"), coming to flow error handling you're handling a specific error from HTTP namespace, whereas the error you get is from MULE namespace of type EXPRESSION, so thats the reason the local error handler is unable to handle that error.
To handle this either use MULE:EXPRESSION or MULE:ANY which can serve as a parent error type for MULE:EXPRSSION and HTTP:NOT_FOUND errors
The MULE namespace is an implicit namespace you can also call it as a parent namespace which can handle any runtime based error.
To better understand errors, their hierarchy and global error handler consider visiting the resource mentioned below.
https://youtu.be/9WcG9teCrvo
I am using Mule 4 and Anypoint Studio 7.
I looking to get a list of objects from an AWS S3 bucket, read each xlsx file and transform the message so it is has some new column names.
When I do this I get an error:
Unable to call any overload of function `Value Selector` with arguments (Binary, "Sheet 1") overloads
How can I resolve this error?
Excel File in S3 Bucket:
Workflow:
<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns:s3="http://www.mulesoft.org/schema/mule/s3"
xmlns:sftp="http://www.mulesoft.org/schema/mule/sftp"
xmlns:ee="http://www.mulesoft.org/schema/mule/ee/core" xmlns:file="http://www.mulesoft.org/schema/mule/file" xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/file http://www.mulesoft.org/schema/mule/file/current/mule-file.xsd
http://www.mulesoft.org/schema/mule/ee/core http://www.mulesoft.org/schema/mule/ee/core/current/mule-ee.xsd
http://www.mulesoft.org/schema/mule/sftp http://www.mulesoft.org/schema/mule/sftp/current/mule-sftp.xsd
http://www.mulesoft.org/schema/mule/s3 http://www.mulesoft.org/schema/mule/s3/current/mule-s3.xsd">
<flow name="aws-flow" doc:id="21bd363a-166e-4fc2-9246-dd6f48763db7" >
<scheduler doc:name="Scheduler" doc:id="4e3bf7a7-99ce-44e3-b732-fece4200c5b6" >
<scheduling-strategy >
<fixed-frequency frequency="10000000"/>
</scheduling-strategy>
</scheduler>
<s3:list-objects doc:name="List objects AWS S3 bucket" doc:id="640ed7a3-2e0f-4c5d-99a1-fd503d02f055" config-ref="Amazon_S3_Configuration" bucketName="${aws.s3.bucket.name}"/>
<foreach doc:name="For Each" doc:id="f0989986-2d2e-4cc9-b51e-d61cf9d01210" >
<choice doc:name="Choice" doc:id="9f47cc0e-d784-4dcb-83a6-07e553997aab" >
<when expression='#[payload.key endsWith ".xlsx"]'>
<s3:get-object doc:name="Get object from AWS S3" doc:id="568ed3c4-d7f8-4657-a515-835e5671b72c" config-ref="Amazon_S3_Configuration" bucketName="#[payload.bucketName]" key="#[payload.key]" />
<ee:transform doc:name="Transform Message" doc:id="966cf917-d50d-4858-8e26-9f381bc57de4">
<ee:message>
<ee:set-payload><![CDATA[%dw 2.0
output application/xlsx
---
{
"Sheet 1": payload[0] map ( sheet1 , indexOfSheet1 ) -> {
EmployeeId: sheet1.EmployeeId default "" as String,
Name: sheet1.Name default "" as String,
}
}]]></ee:set-payload>
</ee:message>
</ee:transform>
<logger level="INFO" doc:name="Log Payload" doc:id="f3738738-d56d-4930-94f8-763aea3b5dbe" message='#[payload]'/>
</when>
<otherwise >
<logger level="INFO" doc:name="Log File Invalid" doc:id="e3f5c9c2-0b75-4b1c-88dc-cb1ae1e2f249" message="Invalid file"/>
</otherwise>
</choice>
</foreach>
</flow>
</mule>
the problem is that the S3 connector is not setting the mime type to "application/xlsx" so DW instead of parsing it as a ExcelFile it takes it as Binary content.
There is a simple solution
after
<s3:get-object doc:name="Get object from AWS S3" doc:id="568ed3c4-d7f8-4657-a515-835e5671b72c" config-ref="Amazon_S3_Configuration" bucketName="#[payload.bucketName]" key="#[payload.key]" />
add
<set-payload value="#[payload]" mimeType="application/xlsx"/>
I am trying to move files with the help of MuleRequester. With large files (300MB>x) sometimes the while flow is blocking and I always get the error message below.
The file is locked by the java.exe process all the time.
With small files there is no problem.
The file remains in the folder "C:\itm\main\datafeeds\backup" and the "C:\itm\main\datafeeds\process\" is empty.
I don't have any idea how to fix this.
Error Message:
Message : Failed to move file "C:\itm\main\datafeeds\backup\my_file.xml" to "C:\itm\main\datafeeds\process\my_file.xml". The file might already exist.
Payload : [{numOfRecords=0}]
Payload Type : java.util.LinkedList
Endpoint : DefaultInboundEndpoint{endpointUri=file:///C:/itm/main/datafeeds/backup/?autoDelete=true, connector=FileConnector
{
name=File
lifecycle=start
this=300a38e7
numberOfConcurrentTransactedReceivers=4
createMultipleTransactedReceivers=true
connected=true
supportedProtocols=[file]
serviceOverrides=<none>
}
, name='endpoint..C.itm.main.datafeeds.backup', mep=ONE_WAY, properties={autoDelete=true}, transactionConfig=Transaction{factory=null, action=INDIFFERENT, timeout=0}, deleteUnacceptedMessages=false, initialState=started, responseTimeout=10000, endpointEncoding=UTF-8, disableTransportTransformer=false}
Timeout : 1000
Element : /poller_product_parse_aff_file/processors/0/0/1 # product_2_parse_aff_file_to_db
--------------------------------------------------------------------------------
Root Exception stack trace:
org.mule.api.DefaultMuleException: Failed to move file "C:\itm\main\datafeeds\backup\my_file.xml" to "C:\itm\main\datafeeds\process\my_file.xml". The file might already exist.
at org.mule.transport.file.FileMessageRequester.moveOrDelete(FileMessageRequester.java:222)
at org.mule.transport.file.FileMessageRequester.doRequest(FileMessageRequester.java:188)
at org.mule.transport.AbstractMessageRequester.request(AbstractMessageRequester.java:94)
at org.mule.transport.AbstractConnector.request(AbstractConnector.java:2259)
at org.mule.endpoint.DefaultInboundEndpoint.request(DefaultInboundEndpoint.java:80)
at org.mule.client.DefaultLocalMuleClient.request(DefaultLocalMuleClient.java:184)
at org.mule.module.MuleRequesterModule.request(MuleRequesterModule.java:64)
...
The mule-requester cfg:
<mulerequester:request resource="${file.RequesterUrl}" timeout="1000" doc:name="Mule Requester" />
Environment:
Operation System: Windows Server 2012R2
JRE: v1.7
Mule Requester: v1.5
Mule Anypoint Studio: Mule Server 3.8.0 CE
When working with such big files the problem mostly in memory available for java. Try to increase available (max) java heap size.
Thank you for your answer!
I've had such heap problems in the past and I guess that is not the problem in this case, because heap problems are throwing a specific error message.
My vm arguments are:
-Xms2048m
-Xmx4096m
-XX:MaxPermSize=2048m
I think you are copying the same file again to the same location.
org.mule.api.DefaultMuleException: Failed to move file "C:\itm\main\datafeeds\backup\my_file.xml" to "C:\itm\main\datafeeds\process\my_file.xml". The file might already exist.
Can You post your full xml config to help further.
Other alternative would be to use file outbound and write back the payload into output file. For just moving files there could be many other alternatives like batch/shell script etc.
Thank You for your help!
Here ist the whole flow:
<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns:vm="http://www.mulesoft.org/schema/mule/vm" xmlns:mulerequester="http://www.mulesoft.org/schema/mule/mulerequester" xmlns:db="http://www.mulesoft.org/schema/mule/db" xmlns:metadata="http://www.mulesoft.org/schema/mule/metadata" xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns:wmq="http://www.mulesoft.org/schema/mule/ee/wmq" xmlns:tracking="http://www.mulesoft.org/schema/mule/ee/tracking"
xmlns:file="http://www.mulesoft.org/schema/mule/file" xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
xmlns:spring="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="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/file http://www.mulesoft.org/schema/mule/file/current/mule-file.xsd
http://www.mulesoft.org/schema/mule/ee/tracking http://www.mulesoft.org/schema/mule/ee/tracking/current/mule-tracking-ee.xsd
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
http://www.mulesoft.org/schema/mule/ee/wmq http://www.mulesoft.org/schema/mule/ee/wmq/current/mule-wmq-ee.xsd
http://www.mulesoft.org/schema/mule/db http://www.mulesoft.org/schema/mule/db/current/mule-db.xsd
http://www.mulesoft.org/schema/mule/mulerequester http://www.mulesoft.org/schema/mule/mulerequester/current/mule-mulerequester.xsd
http://www.mulesoft.org/schema/mule/vm http://www.mulesoft.org/schema/mule/vm/current/mule-vm.xsd">
<file:connector name="File" autoDelete="true" streaming="false" validateConnections="false" doc:name="File" outputAppend="true" moveToDirectory="${file.workingDir}" />
<flow name="poller_product_parse_aff_file" processingStrategy="synchronous">
<poll doc:name="Poll">
<fixed-frequency-scheduler frequency="15" timeUnit="SECONDS"/>
<db:select config-ref="MYSQL_GENERIC_CFG" doc:name="Database">
<db:parameterized-query><![CDATA[SELECT COUNT(*) AS numOfRecords FROM mule_product]]></db:parameterized-query>
</db:select>
</poll>
<choice doc:name="Choice">
<when expression="#[payload.get(0).numOfRecords < 1000]">
<logger message="#[System.getProperty('line.separator')] ############################ Database is free, so start parsing next xml file ###########################:: #[flowVars.fileRequesterUrl]" level="TRACE" doc:name="Logger"/>
<mulerequester:request resource="${file.RequesterUrl}" timeout="30000" doc:name="Mule Requester"/>
<choice doc:name="Choice">
<when expression="#[payload != null && payload.size()>0]">
<logger message="#[System.getProperty('line.separator')] +++ DB is free and file was found" level="TRACE" doc:name="Logger"/>
<flow-ref name="product_parse_aff_file_to_dbFlow" doc:name="product_parse_aff_file_to_dbFlow"/>
</when>
<otherwise>
<logger message="#[System.getProperty('line.separator')] --- File not found or filesize is zero: #[payload]" level="TRACE" doc:name="Logger"/>
</otherwise>
</choice>
</when>
<when expression="">
<logger message="::::::::::::::: start of parsing the files:no fo records in db :::::>#[payload.get(0).numOfRecords]" level="TRACE" doc:name="Logger"/>
</when>
<otherwise>
<logger message="::::::::::file processing not started as the condition is not met i.e SELECT COUNT(*) AS numOfRecords FROM mule_product >1000" level="TRACE" doc:name="Logger"/>
</otherwise>
</choice>
</flow>
....
</mule>
Mule version: 3.7.3
Anypoint Studio: 5.4.0
To learn DataWeave, I've created a simple flow that executes twitter search, loops through each tweet, attempts to use DataWeave to condense the payload, then logs the results. The issue I'm running into is that the json seems to be null coming out of DW.
Tweet Text before DW:
Welcome to the world of #APILedConnectivity Lebron James! Your request has been processed by #MuleSoft #AnypointPlatform # 1986
The payload coming out of DW is not null:
org.mule.api.processor.LoggerMessageProcessor: DW Payload: com.mulesoft.weave.mule.WeaveMessageProcessor$WeaveOutputHandler#1657d772
However, the 'tweet' json element is null when outputting to logs:
org.mule.api.processor.LoggerMessageProcessor: Found Tweet: null
Here's my sample mule flow:
<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns:dw="http://www.mulesoft.org/schema/mule/ee/dw" xmlns:metadata="http://www.mulesoft.org/schema/mule/metadata" xmlns:json="http://www.mulesoft.org/schema/mule/json" xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns:twitter="http://www.mulesoft.org/schema/mule/twitter" xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
xmlns:spring="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="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/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
http://www.mulesoft.org/schema/mule/twitter http://www.mulesoft.org/schema/mule/twitter/current/mule-twitter.xsd
http://www.mulesoft.org/schema/mule/ee/dw http://www.mulesoft.org/schema/mule/ee/dw/current/dw.xsd
http://www.mulesoft.org/schema/mule/json http://www.mulesoft.org/schema/mule/json/current/mule-json.xsd">
<flow name="test-dataweaveFlow">
<http:listener config-ref="HTTP_Listener_Configuration" path="/test" doc:name="HTTP"/>
<twitter:search config-ref="TwitterConnection" query="#[json:hashtag]" doc:name="Twitter"/>
<foreach collection="#[payload.tweets]" doc:name="For Each">
<logger message="Payload before DW: #[payload.text]" level="INFO" doc:name="Logger"/>
<dw:transform-message metadata:id="99e88418-ce6a-4d18-a900-6c35d7f36c83" doc:name="Transform Message">
<dw:input-payload doc:sample="QueryResult.dwl"/>
<dw:set-payload><![CDATA[%dw 1.0
%output application/json
---
{
tweet: {
screenName: payload.user.screenName,
tweetId: payload.id,
tweetContent: payload.text
}
}
]]></dw:set-payload>
</dw:transform-message>
<logger message="DW Payload: #[payload]" level="INFO" doc:name="Logger"/>
<logger message="Found Tweet: #[payload.tweet]" level="INFO" doc:name="Logger"/>
</foreach>
<json:object-to-json-transformer doc:name="Object to JSON"/>
</flow>
</mule>
Screenshot of the mapping:
Any ideas where I might be messing things up?
Don't put the DataWeave transformer in the for each scope!
You can iterate in DataWeave, that will give you a better result probably.
You will create a map from the payload, the map operator accepts a collection.
The twitter connector will give you a collection of JSON objects.
The clue here is, drag the DataWeave transformer after the Twitter connector.
The input data in DataWeave will be provided and you will have autocomplete in you DataWeave script.
Docs: https://docs.mulesoft.com/mule-user-guide/v/3.7/dataweave-reference-documentation
Some examples in how to map form JSON here:
https://docs.mulesoft.com/mule-user-guide/v/3.7/dataweave-examples#json-to-xml
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"/>