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.

This document walks through a few examples that introduce most of the basic implementations of Mule Expression Language (MEL). Each example includes a step-by-step-guide for creating the flow in 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.

Assumptions

This document assumes you are familiar with Mule Expression Language’s [basis syntax] and are comfortable building and running Mule applications using [Mule Studio] or XML.

If you aren’t yet familiar with how to access information about the Mule messages that pass through your applications, consider following this [tutorial], which walks you through both examining your Mule message and its data structure and writing simple MEL expressions.

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, use MEL to:

*Access inbound property *Dynamically set the payload

Studio Visual Editor

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

  2. Open the HTTP endpoint’s properties editor and give it the path greet1. In this way, the endpoint will be reached via the URI http://localhost:8081/greet1.

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

    Hello #[message.inboundProperties.username]

    This expression will capture 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 URL http://localhost:8081/greet1?username=yourName. The response prints the words Hello yourName in your browser.

    This method of accessing query string parameters does not escape URL encoded characters (such as spaces, which would be read as %20). If you want to access the escaped values of these parameters, you can do it through a special inbound property provided by Mule named http.query.params which contains a map, where keys are property names and values are escaped property values.

    In this example, you can get a user name with escaped characters through this expression:

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

XML Editor

  1. In a new flow, add an http:inbound-endpoint and configure it with path greet1.

    <http:inbound-endpoint exchange-pattern="request-response" host="localhost" port="8081" path="greet1" doc:name="HTTP"/>
    Attribute Value

    exchange-pattern

    request-response

    host

    localhost

    port

    8081

    path

    greet1

    doc:name

    HTTP

    In this way, the endpoint will be reached via the URI http://localhost:8081/greet1.

  2. After the endpoint, add a set-payload transformer, configured as shown:

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

    value

    Hello #[message.inboundProperties.username]

    doc:name

    Set Payload

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

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

    <flow name="greetingFlow1" doc:name="greetingFlow1">
            <http:inbound-endpoint exchange-pattern="request-response" host="localhost" port="8081" path="greet1" doc:name="HTTP"/>
            <set-payload value="Hello #[message.inboundProperties.username]" doc:name="Set Payload"/>
        </flow>
  4. Save and run project.

  5. Through a browser, access the URL http://localhost:8081/greet1?username=yourName. This will print the words 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 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 endpoint 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.

  2. Open the HTTP Endpoint’s properties editor and give it the path greet2. In this way, the endpoint will be reached via the URI http://localhost:8081/greet2.

  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.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 individual 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.username == empty]

    This expression accesses the username inbound property and determines whether or not it is null or an empty string. This expression returns neither 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 project.

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

  8. Then, 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:inbound-endpoint

    <http:inbound-endpoint exchange-pattern="request-response" host="localhost" port="8081" path="greet2" doc:name="HTTP"/>
    Attribute Value

    exchange-pattern

    request-response

    host

    localhost

    port

    8081

    path

    greet2

    doc:name

    HTTP

    In this way, the endpoint will be reached via the URI http://localhost:8081/greet2.

  2. After the endpoint, add a choice element with two possible outputs. One of these outputs will be the default, the other will evaluate a MEL expression:

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

    Inside the when tag, write the following MEL expression:

    expression="#[message.inboundProperties.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 return 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.username]

    doc:name

    Set Payload for valid username

    The MEL expression in the second set-property’s value attribute 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.username == empty]">
                    <set-payload value="#['No username provided']" doc:name="Set Payload for invalid username"/>
                </when>
                <otherwise>
                    <set-payload value="Hello #[message.inboundProperties.username]" doc:name="Set Payload for valid username"/>
                </otherwise>
            </choice>
  4. The full code of your flow should look like this:

    <flow name="greetingFlow2" doc:name="greetingFlow2">
            <http:inbound-endpoint exchange-pattern="request-response" host="localhost" port="8081" path="greet2" doc:name="HTTP"/>
            <choice doc:name="Choice">
                <when expression="#[message.inboundProperties.username == empty]">
                    <set-payload value="#['No username provided']" doc:name="Set Payload for invalid username"/>
                </when>
                <otherwise>
                    <set-payload value="Hello #[message.inboundProperties.username]" doc:name="Set Payload for valid username"/>
                </otherwise>
            </choice>
        </flow>
  5. Save and run project.

  6. Through a browser, access the URL http://localhost:8081/greet1?username=yourName. This prints the words Hello yourName in your browser.

  7. Then, 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 will 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, you will 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 endpoint, followed by an Expression component, then a Set Payload component, a File Endpoint, and finally another Set Payload Component.

  2. Open the HTTP Endpoint’s properties editor and give it the Path greet3. In this way, the endpoint will be reached via the URI http://localhost:8081/greet3.

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

    flowVars.username = message.inboundProperties.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 an expression component, it doesn’t need to be surrounded with #[].
  4. In the Set Payload transformer, set the Value to the following MEL expressions:

    #[message.inboundProperties.username], #[message.inboundProperties.age], #[message.inboundProperties.age > 18]

    This will set the payload to a string that contains three comma separated values. The third of these values is evaluating 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 will capture 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 URL http://localhost:8081/greet3?username=yourName&age=22. This will print the words Hello yourName in your browser and also save 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:inbound-endpoint. Configure it as shown:

    <http:inbound-endpoint exchange-pattern="request-response" host="localhost" port="8081" path="greet3" doc:name="HTTP"/>
    Attribute Value

    exchange-pattern

    request-response

    host

    localhost

    port

    8081

    path

    greet3

    doc:name

    HTTP

    In this way, the endpoint will be reached via the URI http://localhost:8081/greet3.

  2. After the endpoint, add an expression component that will use a MEL expression to record the inbound property username into a flowVar.

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

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

    Since this MEL expression is used is 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.username], #[message.inboundProperties.age], #[message.inboundProperties.age &gt;18]" doc:name="Set Payload"/>
    Attribute Value

    value

    #[message.inboundProperties.username], #[message.inboundProperties.age], #[message.inboundProperties.age >18]

    doc:name

    Set Payload

    This will set the payload to a string that contains three comma separated values. The third of these values is evaluating 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 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 earliest in the flow:

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

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

    Attribute Value

    value

    Hello #[flowVars.username]

    doc:name

    Set Payload

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

    <flow name="greetingFlow3" doc:name="greetingFlow3">
            <http:inbound-endpoint exchange-pattern="request-response" host="localhost" port="8081" path="greet3" doc:name="HTTP"/>
            <expression-component doc:name="Expression"><![CDATA[flowVars.username = message.inboundProperties.username]]></expression-component>
            <set-payload value="#[message.inboundProperties.username], #[message.inboundProperties.age], #[message.inboundProperties.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 URL http://localhost:8081/greet3?username=yourNAme*age=22. This will print the words Hello yourName in your browser and also save 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 save 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 will use MEL to:

  • set a flow 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 the new flow, drag an HTTP endpoint, followed by an Expression Component, then a Set Payload transformer, a DataMapper transformer, a File Endpoint, and finally another Set Payload transformer

  2. Open the HTTP endpoint’s properties editor and give it the path greet4. In this way, the endpoint will be reached via the URI http://localhost:8081/greet4.

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

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

    #[['username' : message.inboundProperties.username, 'age' : message.inboundProperties.age]]

    This will set the paylaod to a map that contains two key: value pairs.

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

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

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

  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

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

  9. Click Create Mapping to trigger DataMapper to generate the mappings.

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

    input.age>18

    The resulting mapping should look like this:

  11. In the File endpoint, set a path of your choice for the file to be saved.

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

    Hello #[flowVars.username]

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

  13. Save and run project.

  14. In a browser, access the URL http://localhost:8081/greet4?username=yourName&age=22. This will print the words Hello yourName in your browser and also save 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:inbound-endpoint configured as shown.

    <http:inbound-endpoint exchange-pattern="request-response" host="localhost" port="8081" path="greet4" doc:name="HTTP"/>
    Attribute Value

    exchange-pattern

    request-response

    host

    localhost

    port

    8081

    path

    greet4

    doc:name

    HTTP

    In this way, the endpoint is reached via the URI http://localhost:8081/greet4.

  2. After the endpoint, add an expression component that will use a MEL expression to record the inbound property username into a flow variable of the same time

    <expression-component doc:name="Expression"><![CDATA[flowVars.username = message.inboundProperties.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 value attribute to a MEL expression:

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

    value

    #[['username' : message.inboundProperties.username, 'age' : message.inboundProperties.age]]

    doc:name

    Set Payload

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

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

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

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

  5. In the DataMapper properties editor, configure the following:

    • In the input, select Map<k,v> type and *User Defined structure.

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

  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

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

  9. Click Create Mapping to trigger DataMapper to generate the mapping. 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:

  10. Set Studio’s view back to the XML editor. Below the last component, 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/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 will access 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" doc:name="greetingFlow4">
            <http:inbound-endpoint exchange-pattern="request-response" host="localhost" port="8081" path="greet4" doc:name="HTTP"/>
            <expression-component doc:name="Expression"><![CDATA[flowVars.username = message.inboundProperties.username]]></expression-component>
            <set-payload value="#[['username' : message.inboundProperties.username, 'age' : message.inboundProperties.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 will print the words Hello yourName in your browser and also save a csv file that contains this data, plus the value true for the boolean parameter.

Example 5 - Using Xpath

In all 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 a 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 endpoint, followed by an Expression Component, a Set Payload transformer, a File endpoint, and another Set Payload transformer.

  2. Open the HTTP Endpoint’s properties editor and give it the path greet5. In the way, the endpoint is be reached via the URI http://localhost:8081/greet5.

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

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

    This expression calculates the result of the xpath 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 #[].

    Since the payload is in XML, xpath 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 will set the payload to a string that contains three comma separated values. The third of these values is evaluating a condition and returns true or false depending on the user’s age. Once again, as the payload is in XML, xpath 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 will access 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 endpoint 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 to send a POST via a browser extension such a [Postman] (for Google Chrome) or the [curl] command line utility.
    <user>
      <username>test</username>
      <age>21</age>
    </user>

    This will print the words Hello yourName in your browser and also save 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:inbound-endpoint configured as shown.

    <http:inbound-endpoint exchange-pattern="request-response" host="localhost" port="8081" path="greet5" doc:name="HTTP"/>
    Attribute Value

    exchange-pattern

    request-response

    host

    localhost

    port

    8081

    path

    greet5

    doc:name

    In this way, the endpoint is to be reached via the URI http://localhost:8081/greet5.

  2. After the endpoint, add an Expression Component that will use a MEL expression to record the inbound property username into a flow variable. Because the payload XML file, it must be parsed with xpath.

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

    The expression calculate the result of the xpath 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="#[xpath('/user/username').text], #[xpath('/user/age').text], #[xpath('/user/age').text &gt; 18]" doc:name="Set Payload"/>

    Attribute

    Value

    value

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

    doc:name

    Set Payload

    This will set the payload to a string that contains three comma separated values. The third of these values is evaluating a condition and returns true or false depending on the user’s age. Once again, as the payload is in XML, xpath 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:anme

    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:inbound-endpoint exchange-pattern="request-response" host="localhost" port="8081" path="greet5" doc:name="HTTP"/>
        <expression-component doc:name="Expression"><![CDATA[flowVars.username = xpath('/user/username').text]]></expression-component>
        <set-payload value="#[xpath('/user/username').text], #[xpath('/user/age').text], #[xpath('/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>
  1. Save and run your project.

  2. You must now sent the HTTP endpoint an HTTP request that includes a body with an attached XML file. Send a POST requeest 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 browser extenstion such as [Postman] (for Google Chrome) or the [curl] command line utility.
    <user>
      <username>test</username>
      <age>21</age>
    </user>

    This will print the words Hello yourName in your browser and also save 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 will 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 endpoint, followed by a JSON to Object transformer, an Expression Component, a Set Payload transformer, a File endpoint, and another Set Payload transformer.

  2. Open the HTTP Endpoint’s properties editor and give it the path greet6. In this way, the endpoint is reached via the URI http://localhost:8081/greet6

  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 is evaluating 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 endpoint an HTTP request that includes a body with an attached JSON file. Send a POST request to http://localhost:8081/greet6, attaching 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 will print the words Hello yourName in your browser and also save 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:inbound-endpoint configured as shown.

    <http:inbound-endpoint exchange-pattern="request-response" host="localhost" port="8081" path="greet6" doc:name="HTTP"/>
    Attribute Value

    doc:name

    HTTP

    exchange-pattern

    request-response

    host

    localhost

    path

    greet6

    port

    8081

    In this way, the endpoint is reached via the URI http://localhost:8081/greet6.

  2. After the endpoint, 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 #[].
  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 > 18]

    doc:name

    Set Payload

    This sets the payload to a string that contains three comma-separated values. The third of these values is evaluating 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:inbound-endpoint exchange-pattern="request-response" host="localhost" port="8081" 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 endpoint an HTTP request that includes a body with an attached JSON file.

  9. Send a POST request to http://localhost:8081/greet6, attaching 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 will print the words Hello yourName in your browser and also save 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.5.0 runtime. Please refer to the 3.4.X documentation for a 3.4.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:file="http://www.mulesoft.org/schema/mule/file"
    xmlns:tracking="http://www.mulesoft.org/schema/mule/ee/tracking" 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.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.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/ee/data-mapper http://www.mulesoft.org/schema/mule/ee/data-mapper/current/mule-data-mapper.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">

<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"/>


<!-- Example 1 Start  How to access properties using MEL, how to set a payload dynamically -->
    <flow name="greetingFlow1" doc:name="greetingFlow1">
        <http:inbound-endpoint exchange-pattern="request-response" host="localhost" port="8081" path="greet1" doc:name="HTTP"/>
        <set-payload value="Hello #[message.inboundProperties.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" doc:name="greetingFlow2">
        <http:inbound-endpoint exchange-pattern="request-response" host="localhost" port="8081" path="greet2" doc:name="HTTP"/>
        <choice doc:name="Choice">
            <when expression="#[message.inboundProperties.username == empty]">
                <set-payload value="#['No username provided']" doc:name="Set Payload for invalid username"/>
            </when>
            <otherwise>
                <set-payload value="Hello #[message.inboundProperties.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="docs-greetingFlow3" doc:name="greetingFlow3">
        <http:inbound-endpoint exchange-pattern="request-response" host="localhost" port="8081" path="greet3" doc:name="HTTP"/>
        <expression-component doc:name="Expression"><![CDATA[flowVars.username = message.inboundProperties.username]]></expression-component>
        <set-payload value="#[message.inboundProperties.username], #[message.inboundProperties.age], #[message.inboundProperties.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="docs-greetingFlow4" doc:name="greetingFlow4">
        <http:inbound-endpoint exchange-pattern="request-response" host="localhost" port="8081" path="greet4" doc:name="HTTP"/>
        <expression-component doc:name="Expression"><![CDATA[flowVars.username = message.inboundProperties.username]]></expression-component>
        <set-payload value="#[['username' : message.inboundProperties.username, 'age' : message.inboundProperties.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:inbound-endpoint exchange-pattern="request-response" host="localhost" port="8081" path="greet5" doc:name="HTTP"/>
        <expression-component doc:name="Expression"><![CDATA[flowVars.username = xpath('/user/username').text]]></expression-component>
        <set-payload value="#[xpath('/user/username').text], #[xpath('/user/age').text], #[xpath('/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:inbound-endpoint exchange-pattern="request-response" host="localhost" port="8081" 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

  • Access the Mule Expression Language [Reference] and [Tips]

  • Want to learn more about how to get information about the Mule message so that you can work with it using MEL? Try out this [tutorial].