Since we're on a major migration process of this website, some component documents here are out of sync right now. In the meantime you may want to look at the early version of the new website
https://camel.apache.org/staging/
We would very much like to receive any feedback on the new site, please join the discussion on the Camel user mailing list.
DozerThe dozer: component provides the ability to map between Java beans using the Dozer mapping framework since Camel 2.15.0. Camel also supports the ability to trigger Dozer mappings as a type converter. The primary differences between using a Dozer endpoint and a Dozer converter are:
In order to use the Dozer component, Maven users will need to add the following dependency to their <dependency> <groupId>org.apache.camel</groupId> <artifactId>camel-dozer</artifactId> <version>x.x.x</version> <!-- use the same version as your Camel core version --> </dependency> URI formatThe Dozer component only supports producer endpoints. dozer:endpointId[?options] Where endpointId is a name used to uniquely identify the Dozer endpoint configuration. An example Dozer endpoint URI: from("direct:orderInput"). to("dozer:transformOrder?mappingFile=orderMapping.xml&targetModel=example.XYZOrder"). to("direct:orderOutput"); Options
Using Data Formats with DozerDozer does not support non-Java sources and targets for mappings, so it cannot, for example, map an XML document to a Java object on its own. Luckily, Camel has extensive support for marshalling between Java and a wide variety of formats using data formats. The Dozer component takes advantage of this support by allowing you to specify that input and output data should be passed through a data format prior to processing via Dozer. You can always do this on your own outside the call to Dozer, but supporting it directly in the Dozer component allows you to use a single endpoints to configure any-to-any transformation within Camel. As an example, let's say you wanted to map between an XML data structure and a JSON data structure using the Dozer component. If you had the following data formats defined in a Camel Context: <dataFormats> <json library="Jackson" id="myjson"/> <jaxb contextPath="org.example" id="myjaxb"/> </dataFormats> You could then configure a Dozer endpoint to unmarshal the input XML using a JAXB data format and marshal the mapping output using Jackson. <endpoint uri="dozer:xml2json?marshalId=myjson&unmarshalId=myjaxb&targetModel=org.example.Order"/> Configuring DozerAll Dozer endpoints require a Dozer mapping configuration file which defines mappings between source and target objects. The component will default to a location of META-INF/dozerBeanMapping.xml if the mappingFile or mappingConfiguration options are not specified on an endpoint. If you need to supply multiple mapping configuration files for a single endpoint or specify additional configuration options (e.g. event listeners, custom converters, etc.), then you can use an instance of <bean id="mapper" class="org.apache.camel.converter.dozer.DozerBeanMapperConfiguration"> <property name="mappingFiles"> <list> <value>mapping1.xml</value> <value>mapping2.xml</value> </list> </property> </bean> Mapping ExtensionsThe Dozer component implements a number of extensions to the Dozer mapping framework as custom converters. These converters implement mapping functions that are not supported directly by Dozer itself. Variable MappingsVariable mappings allow you to map the value of a variable definition within a Dozer configuration into a target field instead of using the value of a source field. This is equivalent to constant mapping in other mapping frameworks, where can you assign a literal value to a target field. To use a variable mapping, simply define a variable within your mapping configuration and then map from the VariableMapper class into your target field of choice: <mappings xmlns="http://dozer.sourceforge.net" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://dozer.sourceforge.net http://dozer.sourceforge.net/schema/beanmapping.xsd"> <configuration> <variables> <variable name="CUST_ID">ACME-SALES</variable> </variables> </configuration> <mapping> <class-a>org.apache.camel.component.dozer.VariableMapper</class-a> <class-b>org.example.Order</class-b> <field custom-converter-id="_variableMapping" custom-converter-param="${CUST_ID}"> <a>literal</a> <b>custId</b> </field> </mapping> </mappings> Custom MappingsCustom mappings allow you to define your own logic for how a source field is mapped to a target field. They are similar in function to Dozer customer converters, with two notable differences:
A custom mapping is declared by using the built-in '_customMapping' converter in your mapping configuration. The parameter to this converter has the following syntax: [class-name][,method-name] Method name is optional - the Dozer component will search for a method that matches the input and output types required for a mapping. An example custom mapping and configuration are provided below. public class CustomMapper { // All customer ids must be wrapped in "[ ]" public Object mapCustomer(String customerId) { return "[" + customerId + "]"; } } <mappings xmlns="http://dozer.sourceforge.net" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://dozer.sourceforge.net http://dozer.sourceforge.net/schema/beanmapping.xsd"> <mapping> <class-a>org.example.A</class-a> <class-b>org.example.B</class-b> <field custom-converter-id="_customMapping" custom-converter-param="org.example.CustomMapper,mapCustomer"> <a>header.customerNum</a> <b>custId</b> </field> </mapping> </mappings> Expression MappingsExpression mappings allow you to use the powerful language capabilities of Camel to evaluate an expression and assign the result to a target field in a mapping. Any language that Camel supports can be used in an expression mapping. Basic examples of expressions include the ability to map a Camel message header or exchange property to a target field or to concatenate multiple source fields into a target field. The syntax of a mapping expression is: [language]:[expression] An example of mapping a message header into a target field: <mappings xmlns="http://dozer.sourceforge.net" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://dozer.sourceforge.net http://dozer.sourceforge.net/schema/beanmapping.xsd"> <mapping> <class-a>org.apache.camel.component.dozer.ExpressionMapper</class-a> <class-b>org.example.B</class-b> <field custom-converter-id="_expressionMapping" custom-converter-param="simple:\${header.customerNumber}"> <a>expression</a> <b>custId</b> </field> </mapping> </mappings> Note that any properties within your expression must be escaped with "\" to prevent an error when Dozer attempts to resolve variable values defined using the EL. |