I've searched for a lot of time and I cannot find a way to solve this problem. In my spring application I have a 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);
}
To map the BesoinPoseEntity
into BesoinPoseDTO
However the map function in 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());
}
}
throws a compilation error
.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
I've tried using lambdas and changing types from iterable to list and arrays and back to streams but to no avail, the error persists. The compiler cannot infer that the R type is BesoinPoseDTO
.
Is there anyway to fix this ?
Thanks
You need to create an instance of BesoinPoseMapper
in another word an entry point to the instance once the implementation of your mapper is generated like this:
@Mapper(uses = BesoinPoseTranslator.class)
public interface BesoinPoseMapper {
BesoinPoseMapper INSTANCE = Mappers.getMapper(BesoinPoseMapper.class);
//...
}
Now in your component you can use:
.map(BesoinPoseMapper.INSTANCE::entityToDTO);
or :
private BesoinPoseMapper besoinPoseMapper = BesoinPoseMapper.INSTANCE;
...
.map(besoinPoseMapper::entityToDTO);
Or because you are using spring you can use componentModel = "spring"
:
@Mapper(uses = BesoinPoseTranslator.class, componentModel = "spring")
and then you can inject your mapper in your component as any spring component for example:
@Autowired
private BesoinPoseMapper besoinPoseMapper;