Transformer performs declarative transformation of the message according to the declared Input Type
and/or Output Type
on a route definition. The default camel message implements DataTypeAware
, which holds the message type represented by DataType
.
It is within the route definition that we declare the Input Type
and/or Output Type
. If the Input Type
and/or Output Type
are different from the message type at runtime, the camel internal processor looks for a Transformer. The role of the Transformer is to transform the current message type to the expected message type. Once the message is transformed successfully or if the message is already in the expected type, then the message data type is updated.
Supported Transformers
Transformer | Description |
---|---|
Data Format Transformer | Transforms by using Data Format |
Endpoint Transformer | Transforms by using Endpoint |
Custom Transformer | Transforms by using custom transformer class. |
Transformer Options
Name | Description |
---|---|
scheme | Type of data model such as |
fromType | Data type to transform from. |
toType | Data type to transform to. |
Transformer Examples
Java DSL:
BindyDataFormat bindy = new BindyDataFormat(); bindy.setType(BindyType.Csv); bindy.setClassType(com.example.Order.class); transformer() .fromType(com.example.Order.class) .toType("csv:CSVOrder") .withDataFormat(bindy);
XML DSL:
<dataFormatTransformer fromType="java:com.example.Order" toType="csv:CSVOrder"> <bindy id="csvdf" type="Csv" classType="com.example.Order"/> </dataFormatTransformer>
Customer Transformer
Examples to illustrate custom Transformer class:
Java DSL:
transformer() .fromType("xml") .toType("json") .withJava(com.example.MyCustomTransformer.class);
XML DSL:
<transformers> <customTransformer className="com.example.MyCustomTransformer" fromType="xml" toType="json"/> </transformers>
Declaration of an Endpoint using XSLT component:
Java DSL:
transformer() .fromType("xml:ABCProduct") .toType("xml:XYZProduct") .withUri("xslt:transform.xsl");
XML DSL:
<camelContext id="camel" xmlns="http://camel.apache.org/schema/spring"> <transformers> <endpointTransformer uri="xslt:transform.xsl" fromType="xml:ABCOrder" toType="xml:XYZOrder"/> </transformers> .... </camelContext>