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

前端 未结 4 1234
别那么骄傲
别那么骄傲 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:35

    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)

提交回复
热议问题