Learnerslesson
   JAVA   
  SPRING  
  SPRINGBOOT  
 HIBERNATE 
  HADOOP  
   HIVE   
   ALGORITHMS   
   PYTHON   
   GO   
   KOTLIN   
   C#   
   RUBY   
   C++   




Spring Boot - PUT with @RequestBody in Rest API


To understand PUT method in rest API, let us take the below example.


So far in the previous tutorials we have seen that the school Inspector visited the school and you being the Principal of the school were responsible to provide him all the names of the students of class 5.


He also asked you to add a new student named Kevin, which we have done in the previous tutorial.



Name Roll Age ClassName
John 1 8 5
Paul 2 7 5
Andrew 3 8 5
Kevin 4 9 5

Now, the school Inspector came up with a new requirement.


He asked you to Update the name of the student Kevin to Kevin Killian and also update his age from 9 to 10.


So, you being the Principal, all you need to do is, use the PUT method to update the name and age of Kevin.


And how would you do that?


Let us understand with the below steps :

  1. The first thing we have to do is, create a method named updateStudent() in the Controller class and annotate with @RequestMapping annotation,



    @RestController
    public class HelloWorldController {
    	@Autowired
    	StudentService studentService;
    	@RequestMapping("/students")
    	public List numberOfStudents() {
    		return studentService.studentDetails();
    	}
    	@RequestMapping(method = RequestMethod.PUT, value = "/students/{name}")
    	public void updateStudent(@RequestBody Student student, @PathVariable String name) {
    		studentService.updateExistingStudent(name, student);
    	}
    }

    Spring Boot - PUT with @RequestBody in Rest API


    So, let us understand the updateStudent() method in detail :

    @RequestMapping(method = RequestMethod.PUT, value = "/students/{name}")
    public void updateStudent(@RequestBody Student student, @PathVariable String name) {
    	studentService.updateExistingStudent(name, student);
    }


    In the first line i.e. @RequestMapping annotation, we have a method and a value attribute.

    @RequestMapping(method = RequestMethod.PUT, value = "/students/{name}")


    In technical terms, in order to update the details of Kevin. We need to gather the new details of the student Kevin in JSON format,

    {"name":"Kevin Killian","roll":4,"age":10,"className":5}


    And make a PUT call to the /students/{name} endpoint.

    So, in simple english, we are going to pass the name of Kevin to the /students/{name} endpoint.
    Spring Boot - PUT with @RequestBody in Rest API


    And now that Spring Boot knows we need to change the details of Kevin. Spring Boot expects the updated details of Kevin.

    Which we supply using the JSON format,

    {"name":"Kevin Killian","roll":4,"age":10,"className":5}

  2. Now, to make the PUT call, you can install POSTMAN. It is a free plugin available on chrome browser.



    Then open POSTMAN and follow the four step process :

    1. Select the method as PUT.


      Spring Boot - PUT with @RequestBody in Rest API

    2. Then type the endpoint(i.e. /students/{name}) to which you are going to make the PUT call.



      	localhost:8080/students/Kevin

      Spring Boot - PUT with @RequestBody in Rest API

    3. Then you need to select body -> raw and write the details of the student Kevin in JSON format.



      		{"name":"Kevin Killian","roll":4,"age":10,"className":5}

      Spring Boot - PUT with @RequestBody in Rest API

    4. Next, you have to tell POSTMAN that you are going to make a PUT call. For that Select Headers and in the KEY type Content-Type and in VALUE type application/json.


      Spring Boot - PUT with @RequestBody in Rest API


    But we are not going to execute it now. There is a little work left.

    Now, if you take a look at the addStudent(...) method from the below code,
    Spring Boot - PUT with @RequestBody in Rest API


    There is something @RequestBody Student student and @PathVariable String name in its parameter.

    public void updateStudent(@RequestBody Student student, @PathVariable String name)


    At first let us look at @PathVariable annotation,

    When you type the name Kevin in the endpoint localhost:8080/students/Kevin, Spring Boot takes the name (i.e. Kevin) and places it in the variable name annotated with @PathVariable annotation.
    Spring Boot - PUT with @RequestBody in Rest API


    Now let us look at the other annotation i.e. @RequestBody annotation.

    So, what happens is, when you submit the details of Kevin(As seen above using POSTMAN),

    {"name":"Kevin Killian","roll":4,"age":10,"className":5}


    Spring Boot makes an assumption that the Request body has the details of a Student.
    Spring Boot - PUT with @RequestBody in Rest API


    And Spring Boot directly maps with Student object.
    Spring Boot - PUT with @RequestBody in Rest API


    Now, that you have the updated details of the Student Kevin, you call the updateExistingStudent(...) method of StudentService,
    Spring Boot - PUT with @RequestBody in Rest API


    And the updateExistingStudent(...) method of StudentService class is called.

  3. Now, in the StudentService (Say the class teacher manages the StudentService, as we have seen in previous tutorials),



    @Service
    public class StudentService {
    	List studentList = new ArrayList<>(Arrays.asList(
    		new Student("John", 1, 8, 5),
    		new Student("Paul", 2, 7, 5),
    		new Student("Andrew", 3, 8, 5),
    		new Student("Kevin", 4, 9, 5)
    	));
    	public List studentDetails() {
    		return studentList;
    	}
    	public void updateExistingStudent(String name, Student student) {
    		for (Student studentTemp : studentList) {
    			if (studentTemp.getName().equals(name)) {
    				int i = studentList.indexOf(studentTemp);
    				studentList.set(i, student);
    			}
    		}
    	}
    }
    
    The updateExistingStudent(...) method is called.
    
    public void updateExistingStudent(String name, Student student) {
    	for (Student studentTemp : studentList) {
    		if (studentTemp.getName().equals(name)) {
    			int i = studentList.indexOf(studentTemp);
    			studentList.set(i, student);
    		}
    	}
    }

    Spring Boot - PUT with @RequestBody in Rest API


    The details of the Student(i.e. Kevin) is updated in the List studentList.

    And that's all. We are done updating the details of the student Kevin.

    Just note, we have changed the StudentService a little, which adds the initial details of the student Kevin, along with John, Paul and Andrew.

    List studentList = new ArrayList<>(Arrays.asList(
    	new Student("John", 1, 8, 5),
    	new Student("Paul", 2, 7, 5),
    	new Student("Andrew", 3, 8, 5),
    	new Student("Kevin", 4, 9, 5)
    ));

  4. Now, we can hit the send button in POSTMAN.


    Spring Boot - PUT with @RequestBody in Rest API


    On hitting Send button, Spring Boot picks the name Kevin from the endpoint localhost:8080/students/Kevin at the same time it also picks the new details of Kevin (i.e. {"name":"Kevin Killian","roll":4,"age":10,"className":5}) from the Body of POSTMAN.

  5. And to check if the details of Kevin is added, let us make a GET request to the endpoint localhost:8080/students.



    And we can see that the updated details of Kevin is displayed(As seen in the below image).
    Spring Boot - PUT with @RequestBody in Rest API