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

走远了吗. 提交于 2019-11-29 11:28:57

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));
        }
    }
}

I was able to accomplish what I needed by creating my own custom converters for this. Here's the code:

import org.apache.commons.lang.StringUtils;
import org.dozer.DozerConverter;

public class IfNotBlankConverter extends DozerConverter<String, String> {

    public IfNotBlankConverter() {
        super(String.class, String.class);
    }

    private String getObject(String source, String destination) {
        if (StringUtils.isNotBlank(source)) {
            return source;
        } else {
            return destination;
        }
    }

    @Override
    public String convertTo(String source, String destination) {
        return getObject(source, destination);
    }

    @Override
    public String convertFrom(String source, String destination) {
        return getObject(source, destination);
    }
}

Second one:

import org.dozer.DozerConverter;

public class IfNotNullConverter extends DozerConverter<Object, Object> {

    public IfNotNullConverter() {
        super(Object.class, Object.class);
    }

    @Override
    public Object convertTo(Object source, Object destination) {
        return getObject(source, destination);
    }

    @Override
    public Object convertFrom(Object source, Object destination) {
        return getObject(source, destination);
    }

    private Object getObject(Object source, Object destination) {
        if (source != null) {
            return source;
        } else {
            return destination;
        }
    }

}

For the sake of future readers: the solution with the custom converter did not work for me. It seems that the converters are just being ignored during the mapping.

However, defining custom field mapper did work quite well: (written in java 8)

dozerBeanMapper.setCustomFieldMapper((source, destination, sourceFieldValue, classMap, fieldMapping) ->
            sourceFieldValue == null);

This mapper returns a boolean indicating weather the mapping was done for that field. So, returning true if the object is null, will notify Dozer that the mapping was finished. Sending false will continue the default mapping. (See MappingProcessor.java - mapField method)

you can use the following to bypass null value in xml configuration

<mapping map-null="false">
  <class-a>org.dozer.vo.AnotherTestObject</class-a>
  <class-b>org.dozer.vo.AnotherTestObjectPrime</class-b>    
  <field>
      <a>field4</a>
      <b>to.one</b>
  </field>
 </mapping>   
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!