Using languages with Dozer in Camel

Updated:

Dozer is a cool bit of kit that maps fields from one class to another. You can use it to perform transformations in Camel.

When it’s plugged into Camel, it can also be used to map from a Camel language expression, via custom converters. This support is in the ExpressionMapper class. This means you can use Dozer with things like Simple language, Groovy, OGNL expressions, and so on.

You might find this feature useful if you want to do more complex mapping, or if you want to map things other than simple Java fields - for example, if you want to map the result of method calls.

You just need to set org.apache.camel.component.dozer.ExpressionMapper as the source class in your mapping file, and then add your expression in the custom-converter-param attribute on a field element:

<mapping>
  <class-a>org.apache.camel.component.dozer.ExpressionMapper</class-a>
  <class-b>com.example.TargetObject</class-b>

  <field custom-converter-id="_expressionMapping"
      custom-converter-param="EXPRESSION_GOES_HERE">
    ...
  </field>
</mapping>

Here are some examples:

Camel language Example What it does
Simple custom-converter-param="simple:\${body.customerName}" Use the result of getCustomerName() method on the Exchange body
Groovy custom-converter-param="groovy:return request.headers.Employee" Use the content of the Employee header

Example

Here’s a full example of a Dozer mapping file, showing how to use Groovy and Simple expressions inside the file.

In this example, I’m using the Simple language’s graph-notation support, to access fields on the body (simple:${body.globalId}) and similar support in Groovy (groovy:return request.headers.Employee.type).

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<mappings xmlns="http://dozermapper.github.io/schema/bean-mapping" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://dozermapper.github.io/schema/bean-mapping https://dozermapper.github.io/schema/bean-mapping.xsd">
  <configuration>
    <wildcard>false</wildcard>
  </configuration>
  <mapping>
    <class-a>org.apache.camel.component.dozer.ExpressionMapper</class-a>
    <class-b>com.example.integration.model.OutputRecord</class-b>

    <!-- Using a Simple expression -->
    <!-- Populate the target field with 'globalId' from the body -->
    <field custom-converter-id="_expressionMapping" 
          custom-converter-param="simple:\${body.globalId}">
      <a>expression</a>
      <b>globalId</b>
    </field>

    <!-- Using a Groovy expression -->
    <!-- Populate the target field with a value from headers -->
    <field custom-converter-id="_expressionMapping" 
          custom-converter-param="groovy:return (request.headers.Employee?.type == 'Full-Time') ? request.headers.Employee.name : null">
      <a>expression</a>
      <b>employeeName</b>
    </field>
  </mapping>
</mappings>

Leave a Comment

You can use Markdown in your comment. To write code, indent lines by 4 spaces.