Java spring映射无法推断类型变量R
我找了很多时间,却找不到解决这个问题的方法。在我的spring应用程序中,我有一个Java spring映射无法推断类型变量R,java,spring,java-stream,Java,Spring,Java Stream,我找了很多时间,却找不到解决这个问题的方法。在我的spring应用程序中,我有一个BesoinPoseMapper @FunctionalInterface @Mapper(uses = BesoinPoseTranslator.class) public interface BesoinPoseMapper { @Mappings({ @Mapping(target = "nbCapteurs", source = "valeur&
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
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());
}
}
公共类BesoinPoseController{
@塞特
@自动连线
私人银行存款;
@GetMapping()
public List getBesoinsPose(@RequestParam字符串范围,@RequestParam(required=false)字符串状态){
返回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
.map(BesoinPoseMapper::entityToDTO);
^
必需:函数您需要创建BesoinPoseMapper
的实例,换句话说,一旦生成映射器的实现,就需要创建实例的入口点,如下所示:
@Mapper(uses = BesoinPoseTranslator.class)
public interface BesoinPoseMapper {
BesoinPoseMapper INSTANCE = Mappers.getMapper(BesoinPoseMapper.class);
//...
}
现在,您可以在组件中使用:
.map(BesoinPoseMapper.INSTANCE::entityToDTO);
或:
春天
或者因为您使用的是spring,所以可以使用componentModel=“spring”
:
然后,您可以将映射器作为任何spring组件注入组件中,例如:
@Autowired
private BesoinPoseMapper besoinPoseMapper;
我认为您需要一个BesoinPoseMapper
的实例。当您使用.map(BesoinPoseMapper::entityToDTO)
时,编译器将尝试在流元素的类型上查找entityToDTO()
(在这种情况下,只有besoinPoseRepo.findAll()才能工作)
返回属于BesoinPoseMapper
子类类型的对象,除非BesoinPoseMapper.entityToDTO
是静态的。显然,情况并非如此。正如YCF_L所说,您可能想拥有BesoinPoseMapper
的一个实例并使用.map(mybesoinposemapper实例::entityToDTO)
谢谢你的回答。当我尝试第一种解决方案时,我得到了一个java.lang.ClassNotFoundException:找不到models.mappers.BesoinPoseMapper的实现
,当我尝试第二种解决方案时,编译器告诉我考虑定义一个BesoinPoseMapper类型的bean
我又做错什么了吗?@ErgiS您缺少清理安装代码、生成应用程序实现的步骤mapper@ErgiS你读过这里的例子吗@ErgiS有一个完整的列表,我需要在build.gradle中添加依赖项。它现在可以工作了。我在单元测试中遇到同样的问题,映射器bean没有定义,它必须是一个gradle依赖项我也是。非常感谢你的帮助
@Mapper(uses = BesoinPoseTranslator.class, componentModel = "spring")
@Autowired
private BesoinPoseMapper besoinPoseMapper;