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.