Alter exchange data with the Mustache component

Mustache component can be used to e.g. change a message body for applications expecting specific data. Message will be processed accordingly to the specified Mustache template.

Any type of exchange data can be altered. In this example we'll extract specific data from the JSON array.

Request Body: image.png

Result: image.png

Download this example

Steps:

1. Add Camel Mustache dependency to your Integration Flow

<dependency>
    <groupId>org.apache.camel</groupId>    
    <artifactId>camel-mustache</artifactId>
    <version>x.x.x</version> <!-- use the same version as your Camel core version -->
</dependency>

Please refer to How to build integration flow packages containing additional Camel components

2. Create your Mustache template. Save the file as MUSTACHE in your Integration Flow directory. In this example:

image.png

3. Refer to MUSTACHE file in the integrationFlow.xml

        <route id="main">
            <from id="direct-start" uri="direct:start"/>
            <setBody>
                <simple>${body.replaceAll("\(|\)", "")}</simple>
            </setBody>
            <convertBodyTo id="convert-body-to-string" type="java.lang.String" charset="UTF-8"/>
            <unmarshal><custom ref="json-jackson"/></unmarshal>
            <to uri = "mustache://MUSTACHE"/>
        </route>

Transformed exchange will be available as Out-Body

4. Create Integration Flow package using Maven

Please refer to How to build integration flow packages containing additional Camel components