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.
Join the DZone community and get the full member experience.
Join For FreeThe 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:
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"
}
]
Hope this helps.
Thanks.
Keep learning.
Opinions expressed by DZone contributors are their own.
Comments