List 如何映射列表<;A>;列出<;B>;用推土机?

List 如何映射列表<;A>;列出<;B>;用推土机?,list,map,dozer,List,Map,Dozer,我有一个服务,它返回一个列表,比如a对象。 我想把它转换成一个列表,比如说B对象 我已经定义了从a到B的映射 天真地,我试着去做 List<A> ayes = ... // call to service List<B> bees = dozerMapper.map(ayes, new ArrayList<B>().getClass()); 列表是=…//呼叫服务 List bees=dozerMapper.map(是的,新的ArrayList().getC

我有一个服务,它返回一个
列表,比如
a
对象。 我想把它转换成一个
列表,比如说
B
对象

我已经定义了从
a
B
的映射

天真地,我试着去做

List<A> ayes = ... // call to service
List<B> bees = dozerMapper.map(ayes, new ArrayList<B>().getClass());
列表是=…//呼叫服务
List bees=dozerMapper.map(是的,新的ArrayList().getClass());
但是,
bees
仍然是
a
对象的
列表


我应该怎么做?

我认为您可以使用提示在Dozer中实现这一点,但是我们发现围绕默认映射器创建包装器更容易。下面是一个例子。然后,您可以仅依靠您定义的默认客户转换器/映射器来进行映射

CollectionMapperDecorator custom = new CollectionMapperDecorator(dozerMapper);
Collection<B> bees = custom.mapCollection(ayes, B.class);

public class CollectionMapperDecorator implements Mapper
{
   private Mapper baseMapper;

   public CollectionMapperDecorator(Mapper baseMapper)
   {
      this.baseMapper = baseMapper;      
   }

   public <T> Collection<T> mapCollection(Object[] source, Class<T> destinationClass)
   {
      return mapCollection(Arrays.asList(source), destinationClass);
   }

   public <T> Collection<T> mapCollection(Object[] source, Collection<T> destination, Class<T> destinationClass)
   {
      return mapCollection(Arrays.asList(source), destination, destinationClass);
   }

   public <T> Collection<T> mapCollection(Collection<? extends Object> source, Class<T> destinationClass)
   {      
      return mapCollection(source, null, destinationClass);
   }   

   public <T> Collection<T> mapCollection(Collection<? extends Object> source, Collection<T> destination, Class<T> destinationClass)
   {
      if(destination == null)
         destination = new ArrayList<T>();

      for(Object sourceObj : source)
      {
         destination.add(map(sourceObj, destinationClass));
      }

      return destination;      
   }

   public <T> T map(Object source, Class<T> destinationClass, String mapId) throws MappingException
   {
      return baseMapper.map(source, destinationClass, mapId);
   }

   public <T> T map(Object source, Class<T> destinationClass) throws MappingException
   {
      return baseMapper.map(source, destinationClass);
   }

   public void map(Object source, Object destination, String mapId) throws MappingException
   {
      baseMapper.map(source, destination, mapId);
   }

   public void map(Object source, Object destination) throws MappingException
   {
      baseMapper.map(source, destination);
   }
}
CollectionMapperDecorator custom=新的CollectionMapperDecorator(dozerMapper);
Collection bees=custom.mapCollection(是,B.class);
公共类CollectionMapperDecorator实现映射器
{
私有映射器baseMapper;
公共集合映射器装饰器(映射器baseMapper)
{
this.baseMapper=baseMapper;
}
公共集合mapCollection(对象[]源,类destinationClass)
{
返回mapCollection(Arrays.asList(源),destinationClass);
}
公共集合mapCollection(对象[]源、集合目标、类destinationClass)
{
返回mapCollection(Arrays.asList(源)、destination、destinationClass);
}
公共集合地图集合(集合)