Java mapstruct如何将对象列表转换为接口列表?
我有下面的接口和类Java mapstruct如何将对象列表转换为接口列表?,java,list,mapstruct,Java,List,Mapstruct,我有下面的接口和类 public interface Fruit { ... } public class AppleDto implements Fruit {...} public class AppleEntity { ... } 我已经创建了一个映射器,它将AppleEntity的List转换为AppleDto的List,但我需要返回类型为水果的List @Mapper public interface FruitsMapper { FruitsMapper INSTANC
public interface Fruit { ... }
public class AppleDto implements Fruit {...}
public class AppleEntity { ... }
我已经创建了一个映射器,它将AppleEntity的List
转换为AppleDto的List
,但我需要返回类型为水果的List
@Mapper
public interface FruitsMapper {
FruitsMapper INSTANCE = Mappers.getMapper(FruitsMapper.class);
@IterableMapping(elementTargetType = AppleDto.class)
List<Fruit> entityToFruits(List<AppleEntity> entity);
}
@Mapper
公共接口水果映射器{
水果映射器实例=Mappers.getMapper(水果映射器.class);
@IterableMapping(elementTargetType=AppleDto.class)
列表实体水果(列表实体);
}
它不允许我转换到接口列表并给出错误。有没有合适的方法来实现我的需要?您需要在AppleEntity
和Fruit
之间定义一个映射方法,并通过@BeanMapping\resultType
定义结果类型
在您的情况下,它将如下所示:
@Mapper
public interface FruitsMapper {
FruitsMapper INSTANCE = Mappers.getMapper(FruitsMapper.class);
@BeanMapping(resultType = AppleDto.class)
Fruit map(AppleEntity entity);
List<Fruit> entityToFruits(List<AppleEntity> entity);
}
您需要在AppleEntity
和Fruit
之间定义一个映射方法,并通过@BeanMapping#resultType
在您的情况下,它将如下所示:
@Mapper
public interface FruitsMapper {
FruitsMapper INSTANCE = Mappers.getMapper(FruitsMapper.class);
@BeanMapping(resultType = AppleDto.class)
Fruit map(AppleEntity entity);
List<Fruit> entityToFruits(List<AppleEntity> entity);
}
你会犯什么样的错误?你会犯什么样的错误?