Transform
Camel supports the Message Translator from the EIP patterns.
How can systems using different data formats communicate with each other using messaging?
Use a special filter, a Message Translator, between other filters or applications to translate one data format into another.
The Message Translator can be done in different ways in Camel:
-
Using template-based Components, with the template being the source for how the message is translated
-
Messages can also be transformed using Data Format to marshal and unmarshal messages in different encodings.
This page is documenting the first approach by using Transform EIP.
Options
The Transform eip supports 3 options, which are listed below.
Name | Description | Default | Type |
---|---|---|---|
expression | Required Expression to return the transformed message body (the new message body to use). | ExpressionDefinition | |
fromType | From type used in data type transformation. | String | |
toType | To type used as a target data type in the transformation. | String | |
disabled | Whether to disable this EIP from the route during build time. Once an EIP has been disabled then it cannot be enabled later at runtime. | false | Boolean |
description | Sets the description of this node. | String |
Using Transform EIP
You can use a Transform which uses an Expression to do the transformation:
In the example below we prepend Hello to the message body using the Simple language:
-
Java
-
XML
-
YAML
from("direct:cheese")
.setBody(simple("Hello ${body}"))
.to("log:hello");
<route>
<from uri="direct:cheese"/>
<transform>
<simple>Hello ${body}</simple>
</transform>
<to uri="log:hello"/>
</route>
- from:
uri: direct:cheese
steps:
- transform:
expression:
simple: Hello ${body}
- to:
uri: log:hello
The Transform may also reference a given from/to data type (org.apache.camel.spi.DataType
).
-
Java
-
XML
-
YAML
from("direct:cheese")
.transform(new DataType("myDataType"))
.to("log:hello");
<route>
<from uri="direct:cheese"/>
<transform toType="myDataType"/>
<to uri="log:hello"/>
</route>
- from:
uri: direct:cheese
steps:
- transform:
to-type: myDataType
- to:
uri: log:hello
The example above defines the Transform EIP that uses a target data type myDataType
. The given data type may reference a Transformer that is able to handle the data type transformation.
Users may also specify fromType
in order to reference a very specific transformation from a given data type to a given data type.
What is the difference between Transform and Set Body
The Transform EIP always sets the result on the OUT message body.
Set Body sets the result accordingly to the Exchange Pattern on the Exchange
.