• Empleos
  • Sobre nosotros
  • profesionales
    • Inicio
    • Empleos
    • Cursos y retos
  • empresas
    • Inicio
    • Publicar vacante
    • Nuestro proceso
    • Precios
    • Evaluaciones
    • Nómina
    • Blog
    • Comercial
    • Calculadora de salario

0

356
Vistas
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 3 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

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 3 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Nuestro proceso Comercial
Legal
Términos y condiciones Política de privacidad
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recomiéndame algunas ofertas
Necesito ayuda