I am fairly new to Java so sorry if this is not well described. I was initially able to get the Student list to appear in localhost:8080/api/v1/student when the list was in the StudentController file.
But for best practice, it was advised that it is good to split the files into Controller and Service files and move the list into the StudentService.java file, which is what I did, but now I am receiving an error saying:
APPLICATION FAILED TO START
***************************
Description:
Parameter 0 of constructor in com.example.demo.student.StudentController required a bean of type 'com.example.demo.student.StudentService' that could not be found.
Action:
Consider defining a bean of type 'com.example.demo.student.StudentService' in your configuration.
Process finished with exit code 1
The error specifies "Consider defining a bean of type 'com.example.demo.student.StudentService' in your configuration" but the error doesn't say which file this is specified.
Other similar Stackoverflow questions have solutions that involve import @Component or importing something. I think it may be due to a missing import so I tried adding 'import com.example.demo.student.StudentService' into the StudentController.java file but I'm still getting the error so I've just removed the import line. The files are below.. thanks!
DemoApplication.java
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
StudentController.java
package com.example.demo.student;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
// @RestController makes the bellow class serve Rest endpoints (@GetMapping)
@RestController
@RequestMapping(path = "api/v1/student")
public class StudentController {
//reference
private final StudentService studentService;
// add to constructor
public StudentController(StudentService studentService) {
// Initializing Private Final StudentService variable
this.studentService = studentService;
}
//In order for ths method to be served as a RESTful endpoint, we annotate with:
@GetMapping
//We now have a GetMapping for Studentcontroller
public List<Student> getStudents() {
return studentService.getStudents();
}
}
StudentService.java
package com.example.demo.student;
import java.time.LocalDate;
import java.time.Month;
import java.util.List;
public class StudentService {
public List<Student> getStudents() {
return List.of(
new Student(
1L,
"Mariam",
"mariam.jamal@gmail.com",
LocalDate.of(2000, Month.JANUARY, 5),
21
)
);
}
}