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

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

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 Relatório

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