Using Annotation in Dozer Mapping

Traditional use of Dozer Mapping was done using the XML configurations. But using XML and configuring fields can be a painful job for developers. This also creates code duplication and we need to map the same fields in both to and from classes for the simplest of mappings as well. To avoid this we can use the Annotations feature provide by Dozer.

How to use Annotation in Dozer Mapping

You can use the @Mapping annotation on
a. Getter of the field OR
b. Field directly

Dozer Mappers adds a bi-directional mapping when it finds the field. Annotations also handle the conversions automatically.
Lets see an example where we will see how the fields can be converted using Annotation

Annotation Example

In this example we will see different ways in which mapping can be done using annotation in Dozer mapping.
1. Automatic mapping of fields with same names.
2. Field level use of Annotations
3. Getter method level use of Annotations
4. Automatic type conversion.

Please see the highlighted comments in the below example to show you the various use of Annotations




Annotation in Dozer Mapping
Dozer mapper Annotations Example

When to Avoid Annotation in Dozer Mapping

1. You are mapping classes, which are not under your control, but provided in libraries.
2. The mappings are quite complex and require many configurations.


1. Dozer – Annotation Mapping