How to map collections in Dozer

前端 未结 8 743
悲&欢浪女
悲&欢浪女 2020-12-14 17:25

I\'d like to do something like:

ArrayList objects = new ArrayList();
...
DozerBeanMapper MAPPER = new DozerBeanMapper         


        
8条回答
  •  一整个雨季
    2020-12-14 17:46

    You can implement your own mapper class which will extend dozer mapper. Example: Create a interface that adds additional method to dozer mapper:

    public interface Mapper extends org.dozer.Mapper {
         List mapAsList(Iterable sources, Class destinationClass);
    }
    

    Next step: Write your own Mapper class by implementing above interface.

    add below method to your implementation class:

    public class MyMapper implements Mapper {
        @Override
        public  List mapAsList(Iterable sources, Class destinationClass) {
            //can add validation methods to check if the object is iterable
            ArrayList targets = new ArrayList();
            for (Object source : sources) {
                targets.add(map(source, destinationClass));
            }
            return targets;
        }
        //other overridden methods.
    }
    

    Hope this helps

提交回复
热议问题