Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

800
Vistas
No primary or single unique constructor found for interface com.example.demo.models.IPerson

I was working on trying to make a call to my backend and have set up a simple controller like this.

@RestController
public class HelloController {

    @RequestMapping("/hello")
    public String Hello(IPerson person) {
        return person.sayHello();
    }
}

IPerson is an interface as follows

package com.example.demo.models;

public interface IPerson {

    default String sayHello() {
        return "Hey There";
    }

}

And I have implemented the interface as

package com.example.demo.models;

import lombok.Builder;

@Builder
public class Person implements IPerson{

    String name;
    String age;

}

The call works when I change the parameter in the controller to the implementation of the interface, i.e

@RestController
public class HelloController {

    @RequestMapping("/hello")
    public String Hello(Person person) {
        return person.sayHello();
    }
}

On calling the function with IPerson as parameter I get the following error.

java.lang.IllegalStateException: No primary or single unique constructor found for interface com.example.demo.models.IPerson
    at org.springframework.beans.BeanUtils.getResolvableConstructor(BeanUtils.java:267) ~[spring-beans-5.3.12.jar:5.3.12]
    at org.springframework.web.method.annotation.ModelAttributeMethodProcessor.createAttribute(ModelAttributeMethodProcessor.java:219) ~[spring-web-5.3.12.jar:5.3.12]
    at org.springframework.web.servlet.mvc.method.annotation.ServletModelAttributeMethodProcessor.createAttribute(ServletModelAttributeMethodProcessor.java:85) ~[spring-webmvc-5.3.12.jar:5.3.12]
    at org.springframework.web.method.annotation.ModelAttributeMethodProcessor.resolveArgument(ModelAttributeMethodProcessor.java:147) ~[spring-web-5.3.12.jar:5.3.12]
    at org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:121) ~[spring-web-5.3.12.jar:5.3.12]
    at org.springframework.web.method.support.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.java:179) ~[spring-web-5.3.12.jar:5.3.12]

What I think might be happening is it expects an implementation of IPerson but is not able to find one but I am unsure if this kind of call is even possible and if it is then what configuration I might be missing.

over 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

Spring uses reflection to instantiate a model attribute in a request handling method. It will try to find a default constructor and call that.

By design in java you cannot create an instance of an interface (nor abstract class) but only for concrete classes. Hence your IPerson will fail as that is an interface and your Person will succeed as that is a class with a constructor.

So in short you cannot use interfaces to bind variables to a model object, only concrete classes.

over 4 years ago · Santiago Trujillo Denunciar

0

Try adding the @RequestBody annotation to parameters. Also, I suggest changing the RequestMapping to PostMapping

Example

  @PostMapping("/hello")
    public String Hello(@RequestBody Person person) {
        return person.sayHello();
    }
over 4 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 Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda