How can I tell Dozer to bypass mapping null or empty string values per field using the programming api?

Daniel Kaplan picture Daniel Kaplan · Oct 8, 2013 · Viewed 14.5k times · Source

The faq explains how to do this in XML. This shows how to do it per class using the API. The problem is you have to set it on on the class. What if I only want it on one field? Is this possible?

Answer

cheb1k4 picture cheb1k4 · Aug 10, 2016

You don't need converter. Dozer allows to do it easily:

import static org.dozer.loader.api.TypeMappingOptions.mapNull;
import static org.dozer.loader.api.TypeMappingOptions.mapEmptyString;

...

public Mapper getMapper() {
    DozerBeanMapper mapper = new DozerBeanMapper();
    mapper.addMapping(beanMappingBuilder());
    return mapper;
}

private BeanMappingBuilder beanMappingBuilder() {
    return new BeanMappingBuilder() {
        @Override
        protected void configure() {
            mapping(ClassA.class, ClassB.class, mapNull(false), mapEmptyString(false));
        }
    }
}