This procedure teaches you how to perform CRUD operation (Insert, Update, Select and Delete) in Spring MVC. Source codes is included at the end of this page.
- Create a new web project in Spring Initializr
- Add the web
- Open the Project in Spring Tool Suite
- Create file called StudentResource in the resources package
- Annotate the Class with @RestController and @RequestMapping of /cruddemo
- Write a method to return a string. Annotate this method with GetMapping of /students
- Test this method
- Create the Student class in the models package
- Create hardcoded list of students in the StudentResource Class
- Modify the getStudents Method to return list of Students (List<Student>)
- Create a business service (a class called StudentService in the resources folder and annotate it with the @Service annotation). This is a singleton object.
- Autowire the business service into the StudentResource class (create a private member variable of StudentService type in the StudentResource class)
- Move the hardcoded list of Students into the StudentService
- Create a getAllTopic() method in the StudentService and make it return the hardcoded list of Students.
- In the StudentResource, make the getStudents method call the getAllStudents method of the StudentService class
- Write a method getStudentById to return a particular student in the StudentService class using the stream api
- Write a getStudentById method in the StudentResource to call the getStudentById of the StudentService class to return a single student. Annotate this method with /students/{Id}. This method takes a parameter Id
- Annotate the Id parameter with @PathVariable of @Id
- Write a method addStudent to post a student to the repository. Anotate this request with @PostMapping of /topics
- The addStudent method take a Student parameter. Annotate the the Student parameter with the @RequestBody parameter
- The addStudent method calls the addStudent method of the StudentService (next will write this method in the StudentService class0
- Write the addStudent method in the StudentService class
- Install PostMan Rest Client or Advanced REST Client in Google Chrome
- Test the application (you will have error)
- Modify students in StudentService to new ArrayList which is mutable
- Retest the application. Try to add a new Student
- Write the updateStudent method in the StudentResource file. This is similar to the addStudent method. Add @PutMapping annotation to /topics/{Id}. This method takes two parameters: the Id gotten as a PathVariable and a Student object gotten from the RequestBody
- Create the updateStudent method in the StudentService file (uses a for loop. Create a Student object inside the for loop with the current id. Then check if the id of this new student equals the passed in id. Then use set(Name) and set(Department) to update the student record
- Write the delete method in the StudentResource file. It takes id gotten from the pathVariable as parameter and calls deleteStudent from the StudentService file
- Write the deleteStudent method in the StudentService file and use removeIf() to delete the student from the list
- Test everything.
The Student Class
public class Student { private String Id; private String name; private String department; public Student() { } public Student(String Id, String name, String department) { this.Id = Id; this.name = name; this.department = department; } public String getId() { return Id; } public void setId(String Id) { this.Id = Id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getDepartment() { return department; } public void setDepartment(String department) { this.department = department; } }
The StudentResource File
@RestController @RequestMapping("/cruddemo") public class StudentResource { @Autowired private StudentService studentService; @GetMapping("/students") public List<Student> getStudents() { return studentService.getAllStudents(); } @GetMapping("/students/{Id}") public Student getStudentById(@PathVariable("Id") String Id){ return studentService.getStudentById(Id); } @PostMapping("/students") public void addStudent(@RequestBody Student student) { studentService.addStudent(student); } @PutMapping("/students/{Id}") public void updateStudent(@PathVariable("Id") String Id, @RequestBody Student student) { studentService.updateStudent(Id, student); } @DeleteMapping("/students/{Id}") public void deleteStudent(@PathVariable String Id) { studentService.deleteStudent(Id); } }
The StudentService File
Service public class StudentService { List<Student> students = new ArrayList<> (Arrays.asList( new Student("S1", "Kindson", "Computer Science"), new Student("S2", "Kate", "Business") )); public List<Student> getAllStudents() { return students; } public Student getStudentById(String Id) { Student s = students.stream() .filter(student -> Id.equals(student.getId())) .findAny() .orElse(null); return s; } public void addStudent(Student student) { students.add(student); } public void updateStudent(String id, Student student) { for(Student s: students) { if(id.equals(s.getId())) { s.setDepartment(student.getDepartment()); s.setName(student.getName()); } } }
One thought on “Spring MVC Step by Step Tutorials with Source Codes”