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

808
Views
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 answers
Answer question

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 Report

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