Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

416
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda