Contact Us 1-800-596-4880

Mule Expression Language Examples

Mule Runtime Engine versions 3.5, 3.6, and 3.7 reached End of Life on or before January 25, 2020. For more information, contact your Customer Success Manager to determine how you can migrate to the latest Mule version.

These examples illustrate basic implementations of Mule Expression Language (MEL). Each example includes a step-by-step guide for creating the flow in Anypoint Studio’s visual editor or in XML. You can also jump straight to the complete code for all six examples, which you can copy and manipulate further in your own applications.

Example 1 – Accessing Properties

This example creates a simple web service that takes an HTTP request that includes a username parameter and returns a greeting using that username.

In this example, you use MEL to:

  • Access an inbound property

  • Dynamically set the payload

Studio Visual Editor

  1. In a new flow, drag an HTTP connector and a Set Payload transformer.

    greeting+flow
  2. Open the HTTP connector’s properties editor and give it the path greet1. Then create a Connector Configuration element for it and set its host to localhost and port to 8081. In this way, you can reach the connector via the http://localhost:8081/greet1 URI.

  3. Open the Set Payload’s properties editor and set the value field with the following MEL expression:

    Hello #[message.inboundProperties.'http.query.params'.username]

    This expression captures the inbound property "username", which is passed as a query string parameter when calling the service.

  4. Save and run the project.

  5. Through a web browser, access the http://localhost:8081/greet1?username=yourName URL.

    The response displays Hello yourName in your browser.

XML Editor

  1. In a new flow, add an http:listener and configure it with the path greet1.

    <http:listener config-ref="HTTP_Listener_Configuration" path="greet1" doc:name="HTTP"/>
    Attribute Value

    config-ref

    HTTP_Listener_Configuration

    path

    greet1

    doc:name

    HTTP

    In this way, you can reach the connector with the http://localhost:8081/greet1 URI.

  2. Create a Connector Configuration element for it with a name that matches the referenced element in config-ref. Set the Port to 8081 and the Path to localhost

    <http:listener-config name="HTTP_Listener_Configuration" host="localhost" port="8081" doc:name="HTTP Listener Configuration"/>
    Attribute Value
    name
    HTTP_Listener_Configuration

    host

    ---
    localhost

    port

    8081

    path

    greet1

    doc:name

    HTTP_Listener_Configuration
  3. After the connector, add a set-payload transformer, configured as shown:

    <set-payload value="Hello #[message.inboundProperties.'http.query.params'.username]" doc:name="Set Payload"/>
    Attribute Value

    value

    Hello #[message.inboundProperties.'http.query.params'.username]

    doc:name

    Set Payload

    The MEL expression used in value captures the inbound property username, which is passed as a query string parameter when calling the service.

  4. The full code of your flow should look like this:

    <http:listener-config name="HTTP_Listener_Configuration" host="localhost" port="8081" doc:name="HTTP Listener Configuration"/>
        <flow name="greetingFlow1" doc:name="greetingFlow1">
            <http:listener config-ref="HTTP_Listener_Configuration" path="greet1" doc:name="HTTP"/>
            <set-payload value="Hello #[message.inboundProperties.'http.query.params'.username]" doc:name="Set Payload"/>
        </flow>
  5. Save and run the project.

  6. Through a browser, access the http://localhost:8081/greet1?username=yourName URI.

    This displays Hello yourName in your browser.

Example 2 – Dynamic Routing by Evaluating a Condition

In the previous example, if your call to the service doesn’t include a username parameter, it results in an error. You can prevent this from happening by adding some flow control components. This example includes a Choice Router that verifies if the required parameter is being passed.

In this example, you can use MEL to:

  • Evaluate conditions in a choice component

  • Access an inbound property

  • Dynamically set the payload

Studio Visual Editor

  1. In a new flow, drag an HTTP connector and a Choice Router. Inside this Choice Router, add a Set Payload component in the space provided for the Default action and another Set Payload as a separate branch, as shown below.

    greet2
  2. Open the HTTP Connector’s properties editor and give it the path greet2. In the Connector Configuration field, simply select the same global element you created for the previous example out of the dropdown list. In this way, you can reach the connector via the http://localhost:8081/greet2 URI.

  3. Open the properties editor of the Set Payload transformer that sits in the Default space and set the Display Name to Set Payload for valid username and configure the Value with the following MEL expression:

    Hello #[message.inboundProperties.'http.query.params'.username]

    This expression captures the inbound property "username", which is passed as a query string parameter when calling the service.

  4. Open the properties editor of the other Set Payload transformer (the one that doesn’t sit in the default space) and set the Display Name to Set Payload for invalid username and configure the Value with the expression #['No username provided'].

  5. Open the Choice Router’s properties editor to configure the routing logic. Double-click on row for the non-default route to provide a conditional expression. In the window that opens up, write the following MEL expression:

    #[message.inboundProperties.'http.query.params'.username == empty]

    This expression accesses the username inbound property and determines whether or not it is null or an empty string. This expression returns either Boolean true or false.

    In MEL, the keyword empty tests the emptiness of a value, and returns boolean true for any of the following:

    • null

    • boolean false

    • empty strings or strings with only white space

    • 0 value numeric values

    • empty collections

  6. Save and run the project.

  7. Through a web browser, access the http://localhost:8081/greet2?username=yourName URL. This displays Hello yourName in your browser.

  8. Access the URL again, but this time do not include any parameters. Verify that the expected output is received.

XML Editor

  1. In a new flow, add an <http:listener element, with its path set to greet2 and that references the global element you created in the previous example:

    <http:listener config-ref="HTTP_Listener_Configuration" path="greet2" doc:name="HTTP"/>
    Attribute Value

    config-ref

    HTTP_Listener_Configuration

    path

    greet2

    doc:name

    HTTP

    In this way, you can reach the connector via the http://localhost:8081/greet2 URI.

  2. After the connector, add a choice element with two possible outputs. One of these outputs is the default, the other evaluates a MEL expression.

    <choice doc:name="Choice">
      <when>
    
      </when>
      <otherwise>
    
      </otherwise>
    </choice>

    Inside the when tag, write the following MEL expression:

    expression="#[message.inboundProperties.'http.query.params'.username == empty]"

    This expression accesses the username inbound property and determines whether or not it is null or an empty string. This expression returns either boolean true or false.

    In MEL, the keyword empty tests the emptiness of a value, and returns boolean true for any of the following:` `

    • null

    • boolean false

    • empty strings or strings with only white space

    • 0 value numeric values

    • empty collections

  3. On each of the two paths in the choice router, add a set-payload transformer. In the first set-payload transformer, add the following attributes:

    Attribute Value

    value

    #[No username provided]

    doc:name

    Set Payload for invalid username

    In the second set-payload transformer, use a MEL expression to access the inbound property:

    Attribute Value

    value

    "Hello #[message.inboundProperties.'http.query.params'.username]"

    doc:name

    Set Payload for valid username

    The MEL expression in the value attribute in the second set-property captures the inbound property username, which is passed as a query string parameter when calling the service.

    <choice doc:name="Choice">
      <when expression="#[message.inboundProperties.'http.query.params'.username == empty]">
        <set-payload value="#['No username provided']" doc:name="Set Payload for invalid username"/>
      </when>
      <otherwise>
        <set-payload value="Hello #[message.inboundProperties.'http.query.params'.username]" doc:name="Set Payload for valid username"/>
      </otherwise>
    </choice>
  4. The full code of your flow should look like this:

    <flow name="greetingFlow2" >
            <http:listener config-ref="HTTP_Listener_Configuration" path="greet2" doc:name="HTTP"/>
            <choice doc:name="Choice">
                <when expression="#[message.inboundProperties.'http.query.params'.username == empty]">
                    <set-payload value="#['No username provided']" doc:name="Set Payload for invalid username"/>
                </when>
                <otherwise>
                    <set-payload value="Hello #[message.inboundProperties.'http.query.params'.username]" doc:name="Set Payload for valid username"/>
                </otherwise>
            </choice>
    </flow>
  5. Save and run the project.

  6. Through a browser, access the `http://localhost:8081/greet1?username=yourName ` URL.

    This displays Hello yourName in your browser.

  7. Access the URL again, but this time do not include any parameters. Verify that the expected output is received.

Example 3 – Variable Assignment and Evaluating Conditions

In this example, the service saves a CSV file with user data besides just returning a greeting. The call to the service now include two parameters, username and age. The service stores these two parameters and adds a third boolean parameter that evaluates if the user is underage (if age is > 18).

In this example, use MEL to:

  • Set a flow variable in the message

  • Generate an output based on evaluating the input

  • Access an inbound property

  • Dynamically set the payload

Studio Visual Editor

  1. In a new flow, drag an HTTP connector, followed by an Expression component, then a Set Payload component, a File Endpoint, and finally another Set Payload Component.

    greeting3
  2. Open the HTTP connector’s properties editor and give it the Path greet3. In this way, you can reach the connector via the http://localhost:8081/greet3 URI.

  3. In the expression component, set the following MEL expression:

    flowVars.username = message.inboundProperties.'http.query.params'.username

    This expression takes the value of the inbound property username and sets it as the flow variable username.

    Because this MEL expression is used in an expression component, it doesn’t need to be surrounded with #[] brackets.
  4. In the Set Payload transformer, set the Value to the following MEL expressions:

    #[message.inboundProperties.'http.query.params'.username],
    #[message.inboundProperties.'http.query.params'.age],
    #[message.inboundProperties.'http.query.params'.age > 18]

    This sets the payload to a string that contains three comma-separated values. The third of these values evaluates a condition and returns true or false depending on the user’s age.

  5. In the properties editor of the File endpoint, set a path for the file to be saved.

  6. Open the properties editor of the final Set Payload transformer and set the Value field with the following MEL expression:

    Hello #[flowVars.username]

    This expression captures the flow variable username, which was created by the Expression Component in your flow.

  7. Save and run the project.

  8. Through a web browser, access the http://localhost:8081/greet3?username=yourName&age=22 URL.

    This displays Hello yourName in your browser and also saves a CSV file that contains this data, plus the value true for the boolean parameter.

XML Editor

  1. In a new flow, add an http:listener. Configure it as shown:

    <http:listener config-ref="HTTP_Listener_Configuration" path="greet3" doc:name="HTTP"/>
    Attribute Value

    config-ref

    HTTP_Listener_Configuration

    path

    greet3

    doc:name

    HTTP

    In this way, you can reach the connector at the http://localhost:8081/greet3 URI.

  2. After the connector, add an expression component that uses a MEL expression to record the inbound property username into a flowVar.

    <expression-component doc:name="Expression"><![CDATA[flowVars.username = message.inboundProperties.'http.query.params'.username]]>
    </expression-component>

    This expression takes the value of the inbound property username and sets it as the flow variable username.

    Since this MEL expression is used in an expression component, it doesn’t need to be surrounded with #[] brackets.
  3. Add a Set Payload transformer and set the value field to a MEL expression:

    <set-payload value="#[message.inboundProperties.'http.query.params'.username], #[message.inboundProperties.'http.query.params'.age], #[message.inboundProperties.'http.query.params'.age &gt;18]" doc:name="Set Payload"/>
    Attribute Value

    value

    #[message.inboundProperties.'http.query.params'.username], #[message.inboundProperties.'http.query.params'.age], #[message.inboundProperties.'http.query.params'.age &gt;18]

    doc:name

    Set Payload

    This sets the payload to a string that contains three comma-separated values. The third of these values evaluates a condition and returns true or false depending on the user’s age.

  4. Below, add a file:outbound-endpoint to send this data to a file:

    <file:outbound-endpoint path="path_of_your_choice" responseTimeout="10000" doc:name="File"/>
    Attribute Value
    path

    (Example)

    /Users/AaronMacbook/Downloads

    responseTimeout
    10000
    doc:name
    File
  5. Below, add another Set Payload transformer containing a MEL expression that references the flow variable that you set earlier in the flow:

    <set-payload value="Hello #[flowVars.username]" doc:name="Set Payload"/>

    This expression accesses the flow variable username, which was created by the Expression Component in your flow.

    Attribute Value
    value
    Helo #[flowVars.username]
    doc:name
    Set Payload
  6. The full code of your flow should look like this:

    <flow name="greetingFlow3" >
      <http:listener config-ref="HTTP_Listener_Configuration" path="greet3" doc:name="HTTP"/>
        <expression-component doc:name="Expression"><![CDATA[flowVars.username = message.inboundProperties.'http.query.params'.username]]>
        </expression-component>
        <set-payload value="#[message.inboundProperties.'http.query.params'.username], #[message.inboundProperties.'http.query.params'.age], #[message.inboundProperties.'http.query.params'.age &gt;18]" doc:name="Set Payload"/>
      <file:outbound-endpoint path="path_of_your_choice" responseTimeout="10000" doc:name="File"/>
      <set-payload value="Hello #[flowVars.username]" doc:name="Set Payload"/>
    </flow>
  7. Save and run your project.

  8. In a browser, access the http://localhost:8081/greet3?username=yourName&age=22 URL.

    This displays Hello yourName in your browser and also saves a CSV file that contains this data, plus the value true for the boolean parameter.

Example 4 – Creating Maps and Evaluating Conditions with DataMapper

*Enterprise*

In this example, like in the previous one, the Mule application saves a CSV file with user data and returns a greeting. The call to the service includes two parameters, username and age. The service stores these two parameters and adds a third boolean parameter that evaluates if the user is above a certain age (if age `> 18`). In this case, however, the mapping between input and output fields and the generation of the third field is performed by a DataMapper component.

In this example, you can use MEL to:

  • Set a flow variable in the message

  • Set a map as your message payload

  • Generate an output based on evaluating the input within DataMapper

  • Access an inbound property

  • Dynamically set the payload

Studio Visual Editor

  1. In a new flow, drag an HTTP connector, followed by an Expression Component, then a Set Payload transformer, a DataMapper transformer, a File Endpoint, and finally another Set Payload transformer.

    greeting4
  2. Open the HTTP connector’s properties editor and give it the path greet4 and select the global element you previously created in the Connector Config field. In this way, you can reach the connector via the http://localhost:8081/greet4 URI.

  3. In the expression component, set the following MEL expression:

    flowVars.username = message.inboundProperties.'http.query.params'.username
    Since this MEL expression is used in an expression component, it doesn’t need to be surrounded with #[] brackets.
  4. In the Set Payload transformer, set the Value field to the following MEL expression:

    #[['username' : message.inboundProperties.'http.query.params'.username, 'age' : message.inboundProperties.'http.query.params'.age]]

    This sets the payload to a map that contains two key:value pairs.

  5. In the DataMapper properties editor, configure the fields as shown:

    • In the Input, keep the default Map<k,v> type and select User Defined structure.

    • In the Output, select CSV type and User Defined structure.

      mel+ex+datamapper
  6. Give your map a Name and Type, then create two fields by clicking the green plus sign:

    Name Type

    username

    String

    age

    Integer

    MELex4-input
  7. For the Output, click Edit Fields, then give the output a Name, select a Delimiter, and create three fields, as shown:

    MELex4-output
  8. Click Create Mapping to trigger DataMapper to generate the mapping.

  9. DataMapper now displays the mapping between input and output fields. The third output field (of_age) needs a MEL expression to get its values. Select it and then write the following MEL expression in the input box below:

    input.age>18

    The resulting mapping should look like this:

    mel+ex+datamapper2
  10. In the File endpoint, set a path of your choice for the file to be saved.

  11. Open the properties editor of the final Set Payload transformer and set the Value field with the following:

    Hello #[flowVars.username]

    This expression accesses the flow variable username, which was created by the Expression Component in your flow.

  12. Save and run the project.

  13. In a browser, access the http://localhost:8081/greet4?username=yourName&age=22 URL.

    This displays Hello yourName in your browser and also saves a CSV file that contains this data, plus the value true for the boolean parameter.

XML Editor

  1. In a new flow, add an http:listener configured as shown.

    <http:listener config-ref="HTTP_Listener_Configuration" path="greet4" doc:name="HTTP"/>
    Attribute Value

    config-ref

    HTTP_Listener_Configuration

    path

    greet4

    doc:name

    HTTP

    In this way, you can reach the connector via the http://localhost:8081/greet4 URI.

  2. After the connector, add an expression component that uses a MEL expression to record the inbound property username into a flow variable of the same name.

    <expression-component doc:name="Expression"><![CDATA[flowVars.username = message.inboundProperties.'http.query.params'.username]]>
    </expression-component>
    Since this MEL expression is used in an expression component, it doesn’t need to be surrounded with #[].
  3. Add a set-payload transformer and set the value attribute to a MEL expression:

    <set-payload value="#[['username' :  message.inboundProperties.'http.query.params'.username, 'age' :  message.inboundProperties.'http.query.params'.age]]" doc:name="Set Payload"/>
    Attribute Value

    value

    #[['username' :  message.inboundProperties.'http.query.params'.username, 'age' :  message.inboundProperties.'http.query.params'.age]]

    doc:name

    Set Payload

    This sets the payload to a map of key:value pairs that contains username and age.

  4. Next, add a data-mapper:transform element.

    <data-mapper:transform doc:name="Map To CSV"/>

    To configure the DataMapper, transition to Studio’s Visual Editor.

  5. In the DataMapper properties editor, configure the fields as shown:

    • In the Input, keep the default Map<k,v> type and select User Defined structure.

    • In the Output, select CSV type and User Defined structure.

      mel+ex+datamapper
  6. For the input, click Edit fields to open the Define the Map dialog.

  7. Give your map a Name and Type, then create two fields by clicking the green plus sign:

    Name Type

    username

    String

    age

    Integer

    MELex4-input
  8. For the Output, click Edit Fields, then give the output a Name, select a Delimiter, and create three fields, as shown:

    MELex4-output
  9. DataMapper now displays the mapping between input and output fields. The third output field (of_age) needs a MEL expression to get its values. Select it and write the following MEL expression in the input box below:

    input.age>18

    The resulting mapping should look like this:

    mel+ex+datamapper2
  10. Set Studio’s view back to the XML editor. Below the last component, add file:outbound-endpoint to send this data to a file:

    <file:outbound-endpoint path="path_of_your_choice" responseTimeout="10000" doc:name="File"/>
    Attribute Value

    path

    (Example)

    Users/AaronMacBook/Desktop

    responseTimeout

    10000

    doc:name

    File
  11. Below, add another Set Payload transformer, configured as shown:

    <set-payload value="Hello #[flowVars.username]" doc:name="Set Payload"/>

    This expression accesses the flow variable username, which was created by the Expression Component in your flow.

    Attribute Value

    value

    Hello #[flowVars.username]

    doc:name

    Set Payload
  12. The full code of your flow should look like this:

    <flow name="greetingFlow4" >
            <http:listener config-ref="HTTP_Listener_Configuration" path="greet4" doc:name="HTTP"/>
            <expression-component doc:name="Expression"><![CDATA[flowVars.username = message.inboundProperties.'http.query.params'.username]]></expression-component>
            <set-payload value="#[['username' :  message.inboundProperties.'http.query.params'.username, 'age' :  message.inboundProperties.'http.query.params'.age]]" doc:name="Set Payload"/>
            <data-mapper:transform config-ref="Map_To_CSV" doc:name="Map To CSV"/>
            <file:outbound-endpoint path="path_of_your_choice" responseTimeout="10000" doc:name="File"/>
            <set-payload value="Hello #[flowVars.username]" doc:name="Set Payload"/>
        </flow>
  13. Save and run your project.

  14. In a browser, access the URL http://localhost:8081/greet4?username=yourName&age=22
    This displays Hello yourName in your browser and also saves a CSV file that contains this data, plus the value true for the boolean parameter.

Example 5 – Using Xpath

In all the previous examples, calls to the service were made via GET requests that included query parameters. In this example, the service you create is an API that accepts POST requests with XML bodies. The required XML includes two parameters, username and age. The service stores these two parameters and adds a third boolean parameter that evaluates if the user is above a certain age (if age >18).

In this example, you use MEL to:

  • Set a flow variable in the message

  • Generate an output based on evaluating the input

  • Parse an XML input through an xpath query

  • Dynamically set the payload

Studio Visual Editor

  1. In a new flow, drag an HTTP connector, followed by an Expression Component, a Set Payload transformer, a File endpoint, and another Set Payload transformer.

    greeting+5
  2. Open the HTTP connector’s properties editor and give it the path greet5. In this way, you can reach the connector from http://localhost:8081/greet5 URI.

  3. Open the Expression Component’s properties editor and set the following MEL expression:

    flowVars.username = xpath3('/user/username').text

    This expression calculates the result of the xpath3 function and sets it as the value of the flow variable username.

    Since this MEL expression is used in an expression component, it doesn’t need to be surrounded with #[] brackets.

    Since the payload is in XML, xpath3 is needed to parse it.

  4. In the Set Payload transformer, set the Value field to the following:

    #[xpath3('/user/username').text],
    #[xpath3('/user/age').text],
    #[xpath3('/user/age').text > 18]

    This sets the payload to a string that contains three comma-separated values. The third of these values evaluates a condition and returns true or false depending on the user’s age. Once again, as the payload is in XML, xpath3 is needed to parse it.

  5. In the File endpoint, set a path of your choice to determine where the .csv file should be saved.

  6. Open the properties editor of the final Set Payload transformer and set the Value field with the following:

    Hello #[flowVars.username]

    This expression accesses the flow variable username, which was created by the Expression Component earlier in your flow.

  7. Save and run your project.

  8. You must now send the HTTP connector an HTTP request that includes a body with an attached XML file.

    Send a POST request to http://localhost:8081/greet5 attach an XML to the body of the message. A sample XML is provided below.

    The easiest way to do this is to send a POST via a browser extension such as Postman (for Google Chrome) or the curl command line utility.
    <user>
      <username>test</username>
      <age>21</age>
    </user>

    This displays Hello yourName in your browser and also saves a CSV file that contains this data, plus the value true for the boolean parameter.

XML Editor

  1. In a new flow, add an `http:listener `configured as shown.

    <http:listener config-ref="HTTP_Listener_Configuration" path="greet5" doc:name="HTTP"/>
    Attribute Value

    config-ref

    HTTP_Listener_Configuration

    path

    greet5

    doc:name

    HTTP

    In this way, you can reach the connector at the http://localhost:8081/greet5 URI.

  2. After the connector, add an Expression Component that uses a MEL expression to record the inbound property username into a flow variable. Because the payload is an XML file, it must be parsed with xpath3.

    <expression-component doc:name="Expression"><![CDATA[flowVars.username = xpath3('/user/username').text]]></expression-component>

    This expression calculates the result of the xpath3 function and sets it as the value of the flow variable username.

    Since this MEL expression is used in an expression component, it isn’t surrounded with #[] brackets.
  3. Add a set-payload transformer and set the value attribute to a comma-separated list of MEL expressions:

    <set-payload value="#[xpath3('/user/username').text], #[xpath3('/user/age').text], #[xpath3('/user/age').text &gt; 18]" doc:name="Set Payload"/>
    Attribute Value

    value

    #[xpath3('/user/username').text], #[xpath3('/user/age').text], #[xpath3('/user/age').text &gt; 18]

    doc:name

    Set Payload

    This sets the payload to a string that contains three comma-separated values. The third of these values evaluates a condition and returns true or false depending on the user’s age. Once again, as the payload is in XML, xpath3 is needed to parse it.

  4. Add a file:outbound-endpoint to output the payload into a CSV file.

    <file:outbound-endpoint path="path_of_your_choice" responseTimeout="10000" doc:name="File"/>
    Attribute Value

    path

    (Example)

    Users/AaronMacBook/Downloads

    responseTimeout

    10000

    doc:name

    File
  5. Below, add another set-payload transformer with a value containing a MEL expression that references the flow variable username that you set earlier in the flow:

    <set-payload value="Hello #[flowVars.username]" doc:name="Set Payload"/>
    Attribute Value

    value

    Hello #[flowVars.username]

    doc:name

    Set Payload
  6. The full code of your flow should look like this:

    <flow name="greetingFlow5" doc:name="greetingFlow5">
            <http:listener config-ref="HTTP_Listener_Configuration" path="greet5" doc:name="HTTP"/>
            <expression-component doc:name="Expression"><![CDATA[flowVars.username = xpath3('/user/username').text]]></expression-component>
            <set-payload value="#[xpath3('/user/username').text], #[xpath3('/user/age').text], #[xpath3('/user/age').text &gt; 18]" doc:name="Set Payload"/>
            <file:outbound-endpoint path="path_of_your_choice" responseTimeout="10000" doc:name="File"/>
            <set-payload value="Hello #[flowVars.username]" doc:name="Set Payload"/>
        </flow>
  7. Save and run your project. You must now send the HTTP connector an HTTP request that includes a body with an attached XML file. Send a POST request to http://localhost:8081/greet5, attaching an XML to the body of the message. A sample XML is provided below.

    The easiest way to do this is by sending a POST via a browser extension such as Postman (for Google Chrome) or the curl command line utility.
    <user>
      <username>test</username>
      <age>21</age>
    </user>

    This displays Hello yourName in your browser and also saves a CSV file that contains this data, plus the value true for the boolean parameter.

Example 6 – Working with Java Objects

This example is just like example 5, except that the service now receives JSON inputs rather than of XML.

The JSON input includes two parameters, username and age. The service stores these two parameters and adds a third boolean parameter that evaluates if the user is above a certain age (if age>18). Mule first transforms the JSON object into a Java object so that MEL expressions can access the object’s attributes.

In this example, you can use MEL to:

  • Set a flow variable in the message

  • Generate an output based on evaluating the input

  • Access a Java object’s attributes

  • Dynamically set the payload

Studio Visual Editor

  1. In a new flow, drag an HTTP connector, followed by a JSON to Object transformer, an Expression Component, a Set Payload transformer, a File endpoint, and another Set Payload transformer.

    greeting+6
  2. Open the HTTP connector’s properties editor and give it the path greet6. In this way, you can reach the connector at the http://localhost:8081/greet6 URI.

  3. Open the properties editor of the JSON to Object transformer and click the Advanced tab. Set the Return Class to java.lang.Object. With this configuration, the JSON input becomes a Java object with attributes that can be easily called by using object.attribute notation.

  4. In the expression component, set the following MEL expression that accesses an attribute of the object and sets that as the value of a flow variable called username:

    flowVars.username = payload.username
    Since this MEL expression is used in an expression component, it doesn’t need to be surrounded with #[].
  5. In the Set Payload component, set the Value field to the following comma-separated list of MEL expressions:

    #[payload.username],
    #[payload.age],
    #[payload.age > 18]

    This sets the payload to a string that contains three comma-separated values. The third of these values evaluates a condition and returns true or false depending on the user’s age.

  6. In the File endpoint, set a Path of your choice to determine where the CSV files should be saved.

  7. Open the properties editor of the final Set Payload transformer and set the Value field with the following:

    Hello #[flowVars.username]

    This expression accesses the flow variable username, which was created by the Expression Component earlier in your flow.

  8. Save and run the project.

  9. You must now send the HTTP connector an HTTP request that includes a body with an attached JSON file.

    Send a POST request to http://localhost:8081/greet6, and attach a JSON object the body of the message. A sample JSON is provided below.

    The easiest way to do this is by sending a POST via a browser extension such as Postman (for Google Chrome) or the curl command line utility.
    { "username": "test", "age" : 21 }

    This displays Hello yourName in your browser and also saves a CSV file that contains this data, plus the value true for the boolean parameter.

XML Editor

  1. In a new flow, add an `http:listener `configured as shown.

    <http:listener config-ref="HTTP_Listener_Configuration" path="greet6" doc:name="HTTP"/>
    Attribute Value

    config-ref

    HTTP_Listener_Configuration

    path

    greet6

    doc:name

    HTTP

    In this way, you can reach the connector at the http://localhost:8081/greet6 URI.

  2. After the connector, add a json:json-to-object-transformer.

    <json:json-to-object-transformer doc:name="JSON to Object" returnClass="java.lang.Object"/>
    Attribute Value

    returnClass

    java.lang.Object

    doc:name

    JSON to Object

    With this configuration, the JSON input becomes a Java object with attributes that can be easily called by using object.attribute notation.

  3. After the transformer, add an expression component that uses a MEL expression to access the Java object’s username attribute and assign its value into a flow variable of the same name.

    <expression-component doc:name="Expression"><![CDATA[flowVars.username = payload.username]]></expression-component>
    Since this MEL expression is used in an expression component, it doesn’t need to be surrounded with #[] brackets.
  4. Add a set-payload transformer and set the value attribute to a comma-separated list of MEL expressions:

    <set-payload value="#[payload.username], #[payload.age], #[payload.age &gt; 18]" doc:name="Set Payload"/>
    Attribute Value
    value
    #[payload.username], #[payload.age], #[payload.age &gt; 18]
    doc:name
    Set Payload

    This sets the payload to a string that contains three comma-separated values. The third of these values evaluates a condition and returns true or false depending on the user’s age.

  5. Add a file:outbound-endpoint to output the payload into a csv file.

    <file:outbound-endpoint path="path_of_your_choice" responseTimeout="10000" doc:name="File"/>
    Attribute Value

    path

    (Example)

    Users/AaronMacBook/Downloads

    responseTimeout

    10000

    doc:name

    File
  6. Below, add another set-payload transformer, containing a MEL expression that references a flow variable:

    <set-payload value="Hello #[flowVars.username]" doc:name="Set Payload"/>

    This expression accesses the flow variable username, which was created by the Expression Component earlier in your flow.

    Attribute Value

    value

    Hello #[flowVars.username]

    doc:name

    Set Payload
  7. The full code of your flow should look like this:

    <flow name="greetingFlow6" doc:name="greetingFlow6">
             <http:listener config-ref="HTTP_Listener_Configuration" path="greet6" doc:name="HTTP"/>
            <json:json-to-object-transformer doc:name="JSON to Object" returnClass="java.lang.Object"/>
            <expression-component doc:name="Expression"><![CDATA[flowVars.username = payload.username]]></expression-component>
            <set-payload value="#[payload.username], #[payload.age], #[payload.age &gt; 18]" doc:name="Set Payload"/>
            <file:outbound-endpoint path="users" responseTimeout="10000" doc:name="File"/>
            <set-payload value="Hello #[flowVars.username]" doc:name="Set Payload"/>
        </flow>
  8. Save and run the project. You must now send the HTTP connector an HTTP request that includes a body with an attached JSON file.

    Send a POST request to` http://localhost:8081/greet6`, and attach a JSON object the body of the message. A sample JSON is provided below.

    The easiest way to do this is to send a POST via a browser extension such as Postman (for Google Chrome) or the curl command line utility.
    { "username": "test", "age" : 21 }

    This displays Hello yourName in your browser and also saves a CSV file that contains this data, plus the value true for the boolean parameter.

Full Code for All Examples

For your convenience, you may download the complete project. Note that this project is configured specifically for the Mule 3.6.0 runtime. Refer to the 3.5.X documentation for a 3.5.X compatible version.
<?xml version="1.0" encoding="UTF-8"?>

<mule xmlns:json="http://www.mulesoft.org/schema/mule/json"
    xmlns:data-mapper="http://www.mulesoft.org/schema/mule/ee/data-mapper" xmlns:cxf="http://www.mulesoft.org/schema/mule/cxf"
    xmlns:file="http://www.mulesoft.org/schema/mule/file" xmlns:ws="http://www.mulesoft.org/schema/mule/ws" xmlns:tls="http://www.mulesoft.org/schema/mule/tls" xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns:tracking="http://www.mulesoft.org/schema/mule/ee/tracking" 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.mulesoft.org/schema/mule/json http://www.mulesoft.org/schema/mule/json/current/mule-json.xsd 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/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/tls http://www.mulesoft.org/schema/mule/tls/current/mule-tls.xsd
http://www.mulesoft.org/schema/mule/ee/data-mapper http://www.mulesoft.org/schema/mule/ee/data-mapper/current/mule-data-mapper.xsd">

<data-mapper:config name="new_mapping_grf" transformationGraphPath="new_mapping.grf" doc:name="DataMapper"/>
    <data-mapper:config name="map_to_csv" transformationGraphPath="map_to_csv.grf" doc:name="map_to_csv"/>

<!-- Necessary configuration element used by all examples  -->
<http:listener-config name="HTTP_Listener_Configuration" host="localhost" port="8081" doc:name="HTTP Listener Configuration"/>

<!-- Example 1 Start - How to access properties using MEL, how to set a payload dynamically -->
    <flow name="greetingFlow1" doc:name="greetingFlow1">
        <http:listener config-ref="HTTP_Listener_Configuration" path="greet1" doc:name="HTTP"/>
        <set-payload value="Hello #[message.inboundProperties.'http.query.params'.username]" doc:name="Set Payload"/>
    </flow>

<!-- Example 1 End -->

<!-- Example 2 Start - How to dynamically route messages based on the results of a conditional expression -->
       <flow name="greetingFlow2" >
        <http:listener config-ref="HTTP_Listener_Configuration" path="greet2" doc:name="HTTP"/>
        <choice doc:name="Choice">
            <when expression="#[message.inboundProperties.'http.query.params'.username == empty]">
                <set-payload value="#['No username provided']" doc:name="Set Payload for invalid username"/>
            </when>
            <otherwise>
                <set-payload value="Hello #[message.inboundProperties.'http.query.params'.username]" doc:name="Set Payload for valid username"/>
            </otherwise>
        </choice>
    </flow>
<!-- Example 2 End -->

<!-- Example 3 Start - How to assign variables, how to evaluate a condition -->
    <flow name="greetingFlow3" >
        <http:listener config-ref="HTTP_Listener_Configuration" path="greet3" doc:name="HTTP"/>
        <expression-component doc:name="Expression"><![CDATA[flowVars.username = message.inboundProperties.'http.query.params'.username]]></expression-component>
        <set-payload value="#[message.inboundProperties.'http.query.params'.username], #[message.inboundProperties.'http.query.params'.age], #[message.inboundProperties.'http.query.params'.age &gt;18]" doc:name="Set Payload"/>
        <file:outbound-endpoint path="Path_of_your_choice" responseTimeout="10000" doc:name="File"/>
        <set-payload value="Hello #[flowVars.username]" doc:name="Set Payload"/>
    </flow>
<!-- Example 3 End -->

<!-- Example 4 Start - How to create a map, how to evaluate a condition with DataMapper -->
    <flow name="greetingFlow4" >
        <http:listener config-ref="HTTP_Listener_Configuration" path="greet4" doc:name="HTTP"/>
        <expression-component doc:name="Expression"><![CDATA[flowVars.username = message.inboundProperties.'http.query.params'.username]]></expression-component>
        <set-payload value="#[['username' :  message.inboundProperties.'http.query.params'.username, 'age' :  message.inboundProperties.'http.query.params'.age]]" doc:name="Set Payload"/>
        <data-mapper:transform config-ref="Map_To_CSV" doc:name="Map To CSV"/>
        <file:outbound-endpoint path="path_of_your_choice" responseTimeout="10000" doc:name="File"/>
        <set-payload value="Hello #[flowVars.username]" doc:name="Set Payload"/>
    </flow>
<!-- Example 4 End -->

<!-- Example 5 Start - How to parse XML input with Xpath -->
    <flow name="docs-greetingFlow5" doc:name="greetingFlow5">
        <http:listener config-ref="HTTP_Listener_Configuration" path="greet5" doc:name="HTTP"/>
        <expression-component doc:name="Expression"><![CDATA[flowVars.username = xpath3('/user/username').text]]></expression-component>
        <set-payload value="#[xpath3('/user/username').text], #[xpath3('/user/age').text], #[xpath3('/user/age').text &gt; 18]" doc:name="Set Payload"/>
        <file:outbound-endpoint path="Path_of_your_choice" responseTimeout="10000" doc:name="File"/>
        <set-payload value="Hello #[flowVars.username]" doc:name="Set Payload"/>
    </flow>
<!-- Example 5 End -->

<!-- Example 6 Start - How to parse Java objects -->
    <flow name="greetingFlow6" doc:name="greetingFlow6">
        <http:listener config-ref="HTTP_Listener_Configuration" path="greet6" doc:name="HTTP"/>
        <json:json-to-object-transformer doc:name="JSON to Object" returnClass="java.lang.Object"/>
        <expression-component doc:name="Expression"><![CDATA[flowVars.username = payload.username]]></expression-component>
        <set-payload value="#[payload.username], #[payload.age], #[payload.age &gt; 18]" doc:name="Set Payload"/>
        <file:outbound-endpoint path="Path_of_your_choice" responseTimeout="10000" doc:name="File"/>
        <set-payload value="Hello #[flowVars.username]" doc:name="Set Payload"/>
    </flow>
<!-- Example 6 End -->

</mule>

See Also