Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Parse Template Transformer in Mule

DZone's Guide to

Parse Template Transformer in Mule

Learn how and when to use the Parse Template component in Mule and see an example of a request and response in a Mule flow.

· Integration Zone
Free Resource

Share, secure, distribute, control, and monetize your APIs with the platform built with performance, time-to-value, and growth in mind. Free 90-day trial of 3Scale by Red Hat

The Parse Template component loads a file into the Mule payload. The 'Parse Template' transformer parses a template file that can contain MEL expressions and places the resulting string into the message payload.We can use the payload,flowVars and inboundproperties in the template to create the dynamic content.

  • Use Case 1: When we want to prepare the payload to be used by the Web Service Consumer or for a SOAP request.

  • Use Case 2: When we want to create a template for emailing.

  • Use Case 3: There are other scenarios where it can be useful, such as returning HTML to a client from an HTTP transport. We can load an HTML file into the payload that includes embedded MEL.  

Samples of MEL that we can define in the template:

  • #[message.inboundProperties.'http.query.params'.name]

  • #[flowVars.accountNumber]

  • #[payload[0]['order_no']]

Let’s walk through how to use JSON Schema validator in Mule application. In this example, we are receiving the order through HTTP call from the end user. We are using a parse template to load a file from an external location – a file which, behaving as a template, expects values for "order number," "order description," and "order provisioning date." The parse template extracts info from the Mule message variables to insert as values into the template and set the resulting contents as the message payload.

Mule Flow:Image title

Code:

<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns:json="http://www.mulesoft.org/schema/mule/json"
xmlns:http="http://www.mulesoft.org/schema/mule/http"
xmlns:jms="http://www.mulesoft.org/schema/mule/jms"
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" version="EE-3.8.1"
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/jms http://www.mulesoft.org/schema/mule/jms/current/mule-jms.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:listener-config name="HTTP_Listener_Configuration"
host="localhost" port="8081" basePath="/parse" doc:name="HTTP Listener Configuration" />
<flow name="parseTempate">
<http:listener config-ref="HTTP_Listener_Configuration" path="/parse"/> 
<set-payload mimeType="application/json" value="#[payload]" />
<json:json-to-object-transformer
returnClass="java.lang.Object" doc:name="JSON to Object" />
<parse-template location="responseHtml.template"
doc:name="Parse Template" />
<set-property doc:name="Content Type" propertyName="Content-Type"
value="text/html" />
</flow>
</mule>

responseHtml.template

<html>
        <body>
                <table border=4>
                <tr>
                        <th>Order No</th>
                        <th>Order Description</th>
                         <th>Provisioning Date</th>  
                </tr>
                <tr>
                        <td>#[payload[0]['order_no']]</td>
                        <td>#[payload[0]['order_description']]</td>
                         <td>#[payload[0]['provisioning_date']]</td>
                </tr>
                </table>
        </body>
</html>

To execute this flow, we will have to open the REST client and pass the payload as part of the POST request body. Below are the screenshots of the request and response.

Request:

[
{ 
"order_no" : "199",
"order_description" : "PostpaidOrder",
"provisioning_date" : "28/07/2017"
}
]


Image title

Hope this helps.

Thanks.

Keep learning.

Explore the core elements of owning an API strategy and best practices for effective API programs. Download the API Owner's Manual, brought to you by 3Scale by Red Hat

Topics:
mule ,mulesoft ,mule esb ,integration

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}