【问题标题】:Java spring map can not infer type-variable RJava spring map无法推断类型变量R
【发布时间】:2020-11-26 08:11:04
【问题描述】:

我已经搜索了很多时间,但找不到解决此问题的方法。在我的春季申请中,我有一个BesoinPoseMapper

@FunctionalInterface
@Mapper(uses = BesoinPoseTranslator.class)
public interface BesoinPoseMapper {
    @Mappings({
            @Mapping(target = "nbCapteurs", source = "valeur", qualifiedByName = {"BesoinPoseTranslator", "nbCapteurs"}),
            @Mapping(target = "typologie", source = "typologie", qualifiedByName = {"BesoinPoseTranslator", "typologie"}),
            @Mapping(target = "site", source = "site", qualifiedByName = {"BesoinPoseTranslator", "emplacement"})
    })
    BesoinPoseDTO entityToDTO(BesoinPoseEntity entity);
}

BesoinPoseEntity 映射到BesoinPoseDTO

但是BesoinPoseController中的map函数

public class BesoinPoseController {

    @Setter
    @Autowired
    private BesoinPoseRepository besoinPoseRepo;

    @GetMapping()
    public List<BesoinPoseDTO> getBesoinsPose(@RequestParam String range, @RequestParam(required = false) String status){

        return StreamSupport.stream(besoinPoseRepo.findAll().spliterator(), false)
                .map(BesoinPoseMapper::entityToDTO)
                .collect(Collectors.toList());
    }
}

引发编译错误

                .map(BesoinPoseMapper::entityToDTO);
                ^
  required: Function<? super BesoinPoseEntity,? extends R>
  found: BesoinPose[...]ToDTO
  reason: cannot infer type-variable(s) R
    (argument mismatch; invalid method reference
      cannot find symbol
        symbol:   method entityToDTO(BesoinPoseEntity)
        location: interface BesoinPoseMapper)
  where R,T are type-variables:
    R extends Object declared in method <R>map(Function<? super T,? extends R>)
    T extends Object declared in interface Stream

我尝试使用 lambda 并将类型从可迭代更改为列表和数组,然后再返回到流,但无济于事,错误仍然存​​在。编译器无法推断出 R 类型是 BesoinPoseDTO

有没有办法解决这个问题?

谢谢

【问题讨论】:

  • 我认为您需要一个 BesoinPoseMapper read this 的实例
  • 当您使用.map(BesoinPoseMapper::entityToDTO) 时,编译器将尝试在您的流元素类型上查找entityToDTO()(在这种情况下,只有当besoinPoseRepo.findAll() 返回的对象类型为BesoinPoseMapper 的子类 - 除非 BesoinPoseMapper.entityToDTO 是静态的。显然,情况并非如此。正如 YCF_L 所说,您可能打算拥有 BesoinPoseMapper 的实例并使用 .map(myBesoinPoseMapperInstance::entityToDTO)

标签: java spring java-stream


【解决方案1】:

您需要创建一个BesoinPoseMapper 的实例,换句话说,一旦您的映射器的实现生成如下:

@Mapper(uses = BesoinPoseTranslator.class)
public interface BesoinPoseMapper {
    BesoinPoseMapper INSTANCE = Mappers.getMapper(BesoinPoseMapper.class);
    //...
}

现在您可以在您的组件中使用:

.map(BesoinPoseMapper.INSTANCE::entityToDTO);

或:

private BesoinPoseMapper besoinPoseMapper = BesoinPoseMapper.INSTANCE;

...

.map(besoinPoseMapper::entityToDTO);

春天

或者因为你使用的是spring你可以使用componentModel = "spring":

@Mapper(uses = BesoinPoseTranslator.class, componentModel = "spring")

然后您可以将映射器作为任何弹簧组件注入您的组件中,例如:

@Autowired
private BesoinPoseMapper besoinPoseMapper;

【讨论】:

  • 感谢您的回复。当我尝试第一个解决方案时,我得到一个java.lang.ClassNotFoundException: Cannot find implementation for models.mappers.BesoinPoseMapper,当我尝试第二个解决方案时,编译器告诉我Consider defining a bean of type BesoinPoseMapper 我又做错了什么吗?
  • @ErgiS 看来您缺少干净安装代码,生成映射器的实现
  • @ErgiS 你读过这里的例子吗mapstruct.org
  • @ErgiS 有example here的完整列表
  • 是的,我需要在 build.gradle 中添加依赖项。现在可以了。我在未定义映射器 bean 的单元测试中遇到了同样的问题,它也必须是 gradle 依赖项。非常感谢您的帮助
猜你喜欢
  • 2016-02-04
  • 2019-03-20
  • 2017-08-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-02-18
  • 2020-03-30
相关资源
最近更新 更多