I have a spring boot application connecting to a local mongoDB database. When I try to fetch all records from a collection it returns an empty array.In the output it says connected to port 27017. It does not give me any error while running the application but just returns an empty array when I type http://localhost:8080/courses/ What am I missing here?
Model class
package SpringBoot.Training.Management.Tool.SpringBootTMTCourses.Model;
import org.bson.types.ObjectId;
import org.springframework.data.annotation.Id;
public class Courses {
@Id
public ObjectId _id;
public String courseID;
public String courseName;
public Courses(ObjectId _id, String courseID, String courseName) {
super();
this._id = _id;
this.courseID = courseID;
this.courseName = courseName;
}
public String get_id() {
return _id.toHexString();
}
public void set_id(ObjectId _id) {
this._id = _id;
}
public String getCourseID() {
return courseID;
}
public void setCourseID(String courseID) {
this.courseID = courseID;
}
public String getCourseName() {
return courseName;
}
public void setCourseName(String courseName) {
this.courseName = courseName;
}
}
Controller class
package SpringBoot.Training.Management.Tool.SpringBootTMTCourses.Controller;
import java.util.List;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import SpringBoot.Training.Management.Tool.SpringBootTMTCourses.Model.Courses;
import SpringBoot.Training.Management.Tool.SpringBootTMTCourses.Repository.CoursesRepository;
@RestController
@RequestMapping("/courses")
public class CourseController {
@Autowired
private CoursesRepository repository;
@RequestMapping(value = "/", method = RequestMethod.GET)
public List<Courses> getAllCourses() {
return repository.findAll();
}
@RequestMapping(value = "/{id}", method = RequestMethod.GET)
public Courses getPetById(@PathVariable("id") ObjectId id) {
return repository.findBy_id(id);
}
}
Repository class
package SpringBoot.Training.Management.Tool.SpringBootTMTCourses.Repository;
import org.bson.types.ObjectId;
import org.springframework.data.mongodb.repository.MongoRepository;
import SpringBoot.Training.Management.Tool.SpringBootTMTCourses.Model.Courses;
public interface CoursesRepository extends MongoRepository<Courses, String> {
Courses findBy_id(ObjectId _id);
}
MongoDb output
db.Courses.find().pretty()
{
"_id" : ObjectId("5d29c3a58212eda90db024c4"),
"courseID" : "1",
"courseName" : "C#"
}
{
"_id" : ObjectId("5d29c3a58212eda90db024c5"),
"courseID" : "2",
"courseName" : "Java"
}
{
"_id" : ObjectId("5d29c3a58212eda90db024c6"),
"courseID" : "3",
"courseName" : "JavaScript"
}
The issue arises because the collection name is not explicitly given in your model class, so spring-data derives the collection name from the class name (Courses) into camel case (courses). Since your actual collection is called Courses, no results are found.
You have to either:
courses. ORAnnotate the model class with the correct collection name like so:
@Document(collection = "Courses")
public class Courses {
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With