Nav
You are viewing an older version of this section. Click here to navigate to the latest version.

Mule Expression Language Examples

This document walks through a few simple 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.

Prerequisites

This document assumes you are familiar with Mule Expression Language’s basic 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, you can use MEL to:

  • Access an inbound property

  • Dynamically set the payload

  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, the connector is 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.'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 URL http://localhost:8081/greet1?username=yourName

    The response displays the words Hello yourName in your browser.

  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 from the URI http://localhost:8081/greet1.

  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:

    
           
                   
                
    1
    2
    3
    4
    5
    
    <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 URL http://localhost:8081/greet1?username=yourName
    This displays 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 can use MEL to:

  • Evaluate conditions in a choice component

  • Access an inbound property

  • Dynamically set the payload

  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. By setting the path to greet2, you can access the connector from a browser with 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 URL http://localhost:8081/greet2?username=yourName. This displays the words 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.

  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:

    &lt;http:listener config-ref="HTTP_Listener_Configuration" path="greet2" doc:name="HTTP"/&gt;
    Attribute Value

    config-ref

    HTTP_Listener_Configuration

    path

    greet2

    doc:name

    HTTP

  2. You can reach the connector using the http://localhost:8081/greet2 URI.

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

    
           
                   
                
    1
    2
    3
    4
    5
    6
    7
    8
    
    &lt;choice doc:name="Choice"&gt;
        &lt;when&gt;
    
        &lt;/when&gt;
        &lt;otherwise&gt;
    
        &lt;/otherwise&gt;
    &lt;/choice&gt;

    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

  4. 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 second set-property value attribute captures the inbound property username, which is passed as a query string parameter when calling the service.

    
           
                   
                
    1
    2
    3
    4
    5
    6
    7
    8
    
    &lt;choice doc:name="Choice"&gt;
        &lt;when expression="#[message.inboundProperties.'http.query.params'.username == empty]"&gt;
            &lt;set-payload value="#['No username provided']" doc:name="Set Payload for invalid username"/&gt;
        &lt;/when&gt;
        &lt;otherwise&gt;
            &lt;set-payload value="Hello #[message.inboundProperties.'http.query.params'.username]" doc:name="Set Payload for valid username"/&gt;
        &lt;/otherwise&gt;
    &lt;/choice&gt;
  5. The full code of your flow should look like this:

    
           
                   
                
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    
    &lt;flow name="greetingFlow2" &gt;
      &lt;http:listener config-ref="HTTP_Listener_Configuration" path="greet2" doc:name="HTTP"/&gt;
      &lt;choice doc:name="Choice"&gt;
          &lt;when expression="#[message.inboundProperties.'http.query.params'.username == empty]"&gt;
              &lt;set-payload value="#['No username provided']" doc:name="Set Payload for invalid username"/&gt;
          &lt;/when&gt;
          &lt;otherwise&gt;
              &lt;set-payload value="Hello #[message.inboundProperties.'http.query.params'.username]" doc:name="Set Payload for valid username"/&gt;
          &lt;/otherwise&gt;
      &lt;/choice&gt;
    &lt;/flow&gt;
  6. Save and run the project.

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

    This displays 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.

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 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 is > 18).

In this example, you uses 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

  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. By setting the path to greet3, you can access the connector from a browser with 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 &#x0023;[].
  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 &gt; 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.

  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 URL http://localhost:8081/greet3?username=yourName&age=22

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

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

    &lt;http:listener config-ref="HTTP_Listener_Configuration" path="greet3" doc:name="HTTP"/&gt;
    Attribute Value

    config-ref

    HTTP_Listener_Configuration

    path

    greet3

    doc:name

    HTTP

  2. You can reach the connector using the URI http://localhost:8081/greet3.

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

    
           
                   
                
    1
    2
    
    &lt;expression-component doc:name="Expression"&gt;&lt;![CDATA[flowVars.username = message.inboundProperties.'http.query.params'.username]]&gt;
    &lt;/expression-component&gt;

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

    Because this MEL expression is in an expression component, don’t surround the expression with &#x0023;[] brackets.
  4. Add a Set Payload transformer and set the value field to a MEL expression:

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

    value

    #[message.inboundProperties.'http.query.params'.username], #[message.inboundProperties.'http.query.params'.age], #[message.inboundProperties.'http.query.params'.age &amp;gt;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. Below, add a file:outbound-endpoint to send this data to a file:

    &lt;file:outbound-endpoint path="path_of_your_choice" responseTimeout="10000" doc:name="File"/&gt;
    Attribute Value

    path

    /Users/Me/Downloads (example)

    responseTimeout

    10000

    doc:name

    File

  6. Below, add another Set Payload transformer containing a MEL expression that references the flow variable that you set earlier in the flow:

    &lt;set-payload value="Hello #[flowVars.username]" doc:name="Set Payload"/&gt;

    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

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

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

  9. In a browser, access the URL http://localhost:8081/greet3?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 4 – 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

  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, the connector is reached via the URI http://localhost:8081/greet5.

  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.

    Because this MEL expression is in an expression component, don’t surround the expression with &#x0023;[] 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:

    
           
                   
                
    1
    2
    3
    
    #[xpath3('/user/username').text],
    #[xpath3('/user/age').text],
    #[xpath3('/user/age').text &gt; 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. 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 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 as Postman (for Google Chrome) or the curl command line utility.
    
           
                   
                
    1
    2
    3
    4
    
    &lt;user&gt;
      &lt;username&gt;test&lt;/username&gt;
      &lt;age&gt;21&lt;/age&gt;
    &lt;/user&gt;

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

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

    &lt;http:listener config-ref="HTTP_Listener_Configuration" path="greet5" doc:name="HTTP"/&gt;
    Attribute Value

    config-ref

    HTTP_Listener_Configuration

    path

    greet5

    doc:name

    HTTP

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

  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.

    &lt;expression-component doc:name="Expression"&gt;&lt;![CDATA[flowVars.username = xpath3('/user/username').text]]&gt;&lt;/expression-component&gt;

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

    Because this MEL expression is in an expression component, don’t surround the expression with &#x0023;[] brackets.
  3. Add a set-payload transformer and set the value attribute to a comma-separated list of MEL expressions:

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

    value

    #[xpath3('/user/username').text], #[xpath3('/user/age').text], #[xpath3('/user/age').text &amp;gt; 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. 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.

    &lt;file:outbound-endpoint path="path_of_your_choice" responseTimeout="10000" doc:name="File"/&gt;
    Attribute Value

    path

    Users/Me/Downloads (example)

    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.:

    &lt;set-payload value="Hello #[flowVars.username]" doc:name="Set Payload"/&gt;
    Attribute Value

    value

    Hello #[flowVars.username]

    doc:name

    Set Payload

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

    
           
                   
                
    1
    2
    3
    4
    5
    6
    7
    
    &lt;flow name="greetingFlow5" doc:name="greetingFlow5"&gt;
            &lt;http:listener config-ref="HTTP_Listener_Configuration" path="greet5" doc:name="HTTP"/&gt;
            &lt;expression-component doc:name="Expression"&gt;&lt;![CDATA[flowVars.username = xpath3('/user/username').text]]&gt;&lt;/expression-component&gt;
            &lt;set-payload value="#[xpath3('/user/username').text], #[xpath3('/user/age').text], #[xpath3('/user/age').text &amp;gt; 18]" doc:name="Set Payload"/&gt;
            &lt;file:outbound-endpoint path="path_of_your_choice" responseTimeout="10000" doc:name="File"/&gt;
            &lt;set-payload value="Hello #[flowVars.username]" doc:name="Set Payload"/&gt;
    &lt;/flow&gt;
  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.
    
           
                   
                
    1
    2
    3
    4
    
    &lt;user&gt;
      &lt;username&gt;test&lt;/username&gt;
      &lt;age&gt;21&lt;/age&gt;
    &lt;/user&gt;

    This displays the words 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 – Working with Java Objects

This example is just like example 4, 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

  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, the connector 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
    Because this MEL expression is in an expression component, don’t surround the expression with &#x0023;[] brackets.
  5. In the Set Payload component, set the Value field to the following comma-separated list of MEL expressions:

    
           
                   
                
    1
    2
    3
    
    #[payload.username],
    #[payload.age],
    #[payload.age &gt; 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 connector an HTTP request that includes a body with an attached JSON file.

  10. 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 displays the words Hello yourName in your browser and also saves a CSV file that contains this data, plus the value true for the boolean parameter.

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

    &lt;http:listener config-ref="HTTP_Listener_Configuration" path="greet6" doc:name="HTTP"/&gt;
    Attribute Value

    config-ref

    HTTP_Listener_Configuration

    path

    greet6

    doc:name

    HTTP

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

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

    &lt;json:json-to-object-transformer doc:name="JSON to Object" returnClass="java.lang.Object"/&gt;
    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.

    &lt;expression-component doc:name="Expression"&gt;&lt;![CDATA[flowVars.username = payload.username]]&gt;&lt;/expression-component&gt;
    Because this MEL expression is in an expression component, don’t surround the expression with #[] brackets.
  4. Add a set-payload transformer and set the value attribute to a comma-separated list of MEL expressions:

    &lt;set-payload value="#[payload.username], #[payload.age], #[payload.age &amp;gt; 18]" doc:name="Set Payload"/&gt;
    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 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.

    &lt;file:outbound-endpoint path="path_of_your_choice" responseTimeout="10000" doc:name="File"/&gt;
    Attribute Value

    path

    Users/Me/Downloads (example)

    responseTimeout

    10000

    doc:name

    File

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

    &lt;set-payload value="Hello #[flowVars.username]" doc:name="Set Payload"/&gt;

    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:

    
           
                   
                
    1
    2
    3
    4
    5
    6
    7
    8
    
    &lt;flow name="greetingFlow6" doc:name="greetingFlow6"&gt;
       &lt;http:listener config-ref="HTTP_Listener_Configuration" path="greet6" doc:name="HTTP"/&gt;
      &lt;json:json-to-object-transformer doc:name="JSON to Object" returnClass="java.lang.Object"/&gt;
      &lt;expression-component doc:name="Expression"&gt;&lt;![CDATA[flowVars.username = payload.username]]&gt;&lt;/expression-component&gt;
      &lt;set-payload value="#[payload.username], #[payload.age], #[payload.age &amp;gt; 18]" doc:name="Set Payload"/&gt;
      &lt;file:outbound-endpoint path="users" responseTimeout="10000" doc:name="File"/&gt;
      &lt;set-payload value="Hello #[flowVars.username]" doc:name="Set Payload"/&gt;
    &lt;/flow&gt;
  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, 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 displays the words 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 and newer. Refer to the Mule Expression Language Examples - 3.5 documentation for a 3.5.X compatible version.

         
      
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
<?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

  • 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