Dozer – Bean mapping

Mapping frameworks helps to structure beans mapping code and to avoid a mess of nonfunctional code all over the application classes.

Dozer is commonly used mapping framework. It provides deferent beans mappings modes (XML, API and annotation). But if you have a complex beans mapping case, you may need to use Dozer API with a custom mapping class.


The aim of this post is to provide a solution a tricky java mapping case using custom converter.

Add dependency


Source classes

public class User {

    private String name;
    private Address address;
    private List<Authority> authorities;

    // Add getters and setters

public class Address {

    private String street;
    private String city;
    private String state;
    private String zipCode;

    public String toString() {
        return street + ", " + city + ", " + state + ", " + zipCode;
    // Add getters and setters

public class Authority {
    private String code;
    private String value;
    // Add getters and setters

Destination classes

The destination class has a string type address and a set of string for authorities values.

The mapper have to do three actions:

  1. Copy name field value.
  2. Convert Address object to String.
  3. Convert List to Set.

public class UserDto {

    private String name;
    private String address;
    private Set<String> authorities;

    // Add getters and setters

Mapping class

The code source bellow prentes the mapping class code.

public class UserToDtoMapping extends BeanMappingBuilder {
    protected void configure() {
        mapping(User.class, UserDto.class)
                .fields("name", "name")
                .fields("address", "address")
                .fields("authorities", "authorities", 

    public static class AuthoritiesConverter implements CustomConverter {
        public Object convert(Object existingDestinationFieldValue, Object sourceFieldValue, Class<?> destinationClass, Class<?> sourceClass) {
            Set<String> values = new HashSet<String>();
            List<Authority> authorities = (List<Authority>)sourceFieldValue;

            if(CollectionUtils.isEmpty(authorities)) {
                return values;

            for(Authority a : authorities) {
            return values;


User name field is a basic case; it will be copied from source to destination without any extra code.
For the address, Dozer will call toString method to convert Address bean to String value, that’s why we override the toString method.
Converting the authorities List to a String Set needs a CustomConverter implementation.

Test beans mapping

public class MappingTest {
    private DozerBeanMapper mapper = new DozerBeanMapper();

    public void init() {
        mapper.addMapping(new UserToDtoMapping());

    public void testMapping() {
        Address address = new Address( "street", "city", "state", "zipCode");
        User a = new User("name", address, Arrays.asList(new Authority("1", "admin"), new Authority("2", "user")));
        UserDto b  =, UserDto.class);
        Assert.assertEquals("name", b.getName());
        Assert.assertEquals("street, city, state, zipCode", b.getAddress());
        Assert.assertTrue(b.getAuthorities().containsAll(Arrays.asList("admin", "user")));

Dozer is great bean mapping framework, but it has some drawbacks like performance issue and mapping error detection at code execution.

If you are looking for Dozer alternative to get better performance and earlier mapping error detection, you have to consider new beans mapping frameworks like MapStruct and Orika.

2 thoughts on “Dozer – Bean mapping

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s