Spring MVC Step by Step Tutorials with Source Codes

Spring MVC Step by Step Tutorial

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.

  1. Create a new web project in Spring Initializr
  2. Add the web
  3. Open the Project in Spring Tool Suite
  4. Create file called StudentResource in the resources package
  5. Annotate the Class with @RestController and @RequestMapping of /cruddemo
  6. Write a method to return a string. Annotate this method with GetMapping of /students
  7. Test this method
  8. Create the Student class in the models package
  9. Create hardcoded list of students in the StudentResource Class
  10. Modify the getStudents Method to return list of Students (List<Student>)
  11. Create a business service (a class called StudentService in the resources folder and annotate it with the @Service annotation). This is a singleton object.
  12. Autowire the business service into the StudentResource class (create a private member variable of StudentService type in the StudentResource class)
  13. Move the hardcoded list of Students into the StudentService
  14. Create a getAllTopic() method in the StudentService and make it return the hardcoded list of Students.
  15. In the StudentResource, make the getStudents method call the getAllStudents method of the StudentService class
  16. Write a method getStudentById to return a particular student in the StudentService class using the stream api
  17. 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
  18. Annotate the Id parameter with @PathVariable of @Id
  19. Write a method addStudent to post a student to the repository. Anotate this request with @PostMapping of /topics
  20. The addStudent method take a Student parameter. Annotate the the Student parameter with the @RequestBody parameter
  21. The addStudent method calls the addStudent method of the StudentService (next will write this method in the StudentService class0
  22. Write the addStudent method in the StudentService class
  23. Install PostMan Rest Client or Advanced REST Client in Google Chrome
  24. Test the application (you will have error)
  25. Modify students in StudentService to new ArrayList which is mutable
  26. Retest the application. Try to add a new Student
  27. 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
  28. 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
  29. Write the delete method in the StudentResource file. It takes id gotten from the pathVariable as parameter and calls deleteStudent from the StudentService file
  30. Write the deleteStudent method in the StudentService file and use removeIf() to delete the student from the list
  31. 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 Comment on “Spring MVC Step by Step Tutorials with Source Codes”

Leave a Reply

Your email address will not be published. Required fields are marked *