Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/381.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/svg/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java mapstruct如何将对象列表转换为接口列表?_Java_List_Mapstruct - Fatal编程技术网

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

你会犯什么样的错误?你会犯什么样的错误?