Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

411
Views
Java spring map can not infer type-variable R

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

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

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

Spring

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;

  • 3.2. Mapping Composition (experimental)
  • 4.2. Using dependency injection
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!