Transformer
Transformer (org.apache.camel.spi.Transformer
) performs declarative transformation of the message according
to the declared Input Type and/or Output Type on a route definition which declares
the expected message type. The default camel Message implements DataTypeAware
, which allows to hold the message type
represented by DataType
.
If the input type and/or output type is declared by Input Type and/or Output Type in the route
definition, and it is different from actual message type at runtime, camel internal processor
looks for a Transformer
which transforms from the current message type to the expected message
type and apply. Once transform succeed or message is already in expected type, then the message
data type is updated.
Data type format
scheme:name
where scheme is the type of data model like java
, xml
or json
, and name is the individual
data type name. If you only specify scheme then it hits all the data types which has that scheme like
a wildcard.
Supported Transformers
Transformer | Description |
---|---|
Data Format Transformer |
Transform with using Data Format |
Endpoint Transformer |
Transform with using Endpoint |
Custom Transformer |
Transform with using custom transformer class. Transformer must be a subclass of |
Common Options
All transformers have following common options to specify which data type is supported by the transformer. scheme
or both of fromType
and toType
must be specified.
DataFormat Transformer Options
Name | Description |
---|---|
type |
Data Format type |
ref |
reference to the Data Format ID |
Here is an example to specify Bindy DataFormat type:
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>
Endpoint Transformer Options
Name | Description |
---|---|
ref |
Reference to the Endpoint ID |
uri |
Endpoint URI |
Here is an example to specify endpoint URI in Java DSL:
transformer()
.fromType("xml")
.toType("json")
.withUri("dozer:myDozer?mappingFile=myMapping.xml...");
And here is an example to specify endpoint ref in XML DSL:
<endpointTransformer ref="myDozerEndpoint" fromType="xml" toType="json"/>
Custom Transformer Options
Note that Transformer must be a subclass of org.apache.camel.spi.Transformer
Name | Description |
---|---|
ref |
Reference to the custom Transformer bean ID |
className |
Fully qualified class name of the custom Transformer class |
Here is an example to specify custom Transformer class:
Java DSL:
transformer()
.fromType("xml")
.toType("json")
.withJava(com.example.MyCustomTransformer.class);
XML DSL:
<customTransformer className="com.example.MyCustomTransformer" fromType="xml" toType="json"/>
Example
For example to declare the Endpoint Transformer which uses
xslt component to transform from xml:ABCOrder
to xml:XYZOrder
, we can do as follows:
Java DSL:
transformer()
.fromType("xml:ABCOrder")
.toType("xml:XYZOrder")
.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>
If you have following route definition, above transformer will be applied when direct:abc
endpoint sends the message to direct:xyz
:
Java DSL:
from("direct:abc")
.inputType("xml:ABCOrder")
.to("direct:xyz");
from("direct:xyz")
.inputType("xml:XYZOrder")
.to("somewhere:else");
XML DSL:
<camelContext id="camel" xmlns="http://camel.apache.org/schema/spring">
<route>
<from uri="direct:abc"/>
<inputType urn="xml:ABCOrder"/>
<to uri="direct:xyz"/>
</route>
<route>
<from uri="direct:xyz"/>
<inputType urn="xml:XYZOrder"/>
<to uri="somewhere:else"/>
</route>
</camelContext>
See Also
The Validator is a related functionality.