No primary or single unique constructor found for interface com.example.demo.models.IPerson
Asked Answered
C

2

12

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.

Corporation answered 15/12, 2021 at 15:9 Comment(3)
Spring uses reflection to create an instance, you cannot create an instance of an interface.Musician
How should Spring know which Implementation of your interface should be instantiated? If you do not instruct it somehow it will not happen magically. You should instruct it by annotating your classes with proper annotationsElectrophilic
Got it, interfaces by design cannot be instantiated so when the request reaches the backend it is unable to create an instance for this IPerson interface but can do for its implementation Person.Corporation
M
2

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.

Musician answered 16/12, 2021 at 10:36 Comment(0)
C
1

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();
    }
Company answered 20/1, 2022 at 21:37 Comment(2)
@RestController already adds the same behavior as @RequestBody.Adolpho
I missed adding @RequestBody in my POST controller and got same error, my rest controller was taking List<Person> as input, & here List is interface, so I think that was I got this error.Jeraldinejeralee

© 2022 - 2024 — McMap. All rights reserved.