javaspringspring-bootspring-mvcspring-security

ERROR: No primary or single unique constructor found for interface com.springdemo.springApplication.Service.Coach


I am new to Spring Boot. Getting Error in Dependency Injection. Here are the classes.

DemoRestController

package com.springdemo.springApplication.Rest;

import com.springdemo.springApplication.Service.Coach;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class DemoRestController {

    @Autowired
    Coach mycoach;

//    @Autowired
//    demoRestController(Coach mycoach){
//        this.mycoach = mycoach;
//    }

//    @GetMapping("/")
//    public String getHelloWorld(){
//        return "Hello World!";
//
//    }

      @GetMapping("/getCoach")
      public String getCoachInfo(Coach mycoach){
         return mycoach.getCoachInfo();
    }

}

Coach.java

public interface Coach {
   public String getCoachInfo();
}

CricketCoach.java

package com.springdemo.springApplication.Service;

import org.springframework.stereotype.Component;

@Component
public class CricketCoach implements Coach {

    @Override
    public String getCoachInfo() {
        return "Here is your coach info !!!!!";
    }
}

Error Logs

2023-12-02T09:31:40.253+05:30 ERROR 1521 --- [nio-8080-exec-2] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed: java.lang.IllegalStateException: No primary or single unique constructor found for interface com.springdemo.springApplication.Service.Coach] with root cause

java.lang.IllegalStateException: No primary or single unique constructor found for interface com.springdemo.springApplication.Service.Coach
    at org.springframework.beans.BeanUtils.getResolvableConstructor(BeanUtils.java:269) ~[spring-beans-6.0.14.jar:6.0.14]
    at org.springframework.web.method.annotation.ModelAttributeMethodProcessor.createAttribute(ModelAttributeMethodProcessor.java:221) ~[spring-web-6.0.14.jar:6.0.14]
    at org.springframework.web.servlet.mvc.method.annotation.ServletModelAttributeMethodProcessor.createAttribute(ServletModelAttributeMethodProcessor.java:85) ~[spring-webmvc-6.0.14.jar:6.0.14]
    at 

Getting Error in dependency injection, applied @Component annotation in Cricketcoach class.


Solution

  • The issue is that for some reason, you commented out our controller constructor and you are putting your interface in the getDailyWorkout method, which is an incorrect constructor injection. You are trying to call a method that you defined in your interface with an argument while there is no argument in the signature. You need to inject your Coach interface into DemoController class, not your method.

    It should be something like

    @RestController
    public class DemoRestController { //<-- Uppercase first letter of the class
    
        private Coach myCoach;
    
        @Autowired
        public DemoRestController(Coach theCoach) { //<-- Uppercase first letter of the class
            myCoach = theCoach; //<-- do not use same variable name
        // otherwise you will get "this.myCoach is null exception"
        }
    
    //    @GetMapping("/")
    //    public String getHelloWorld() {
    //        return "Hello World!";
    //
    //    }
    
        @GetMapping("/getCoach")
        public String getCoachInfo() { //<-- Removed argument 
           return myCoach.getCoachInfo();
        }
    }
    

    Also please DO NOT name your classes with the first letter in lowercase. It should be DemoRestController not demoRestController when you are defining a constructor.

    After that you can visit http://localhost:8080/getCoach