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

前端 未结 4 1226
别那么骄傲
别那么骄傲 2020-12-19 10:02

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

4条回答
  •  悲&欢浪女
    2020-12-19 10:50

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

提交回复
热议问题