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




Spring Boot - POST with @RequestBody in Rest API


To understand post 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.



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

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


He said that there is a new student named Kevin, whose age is 9 and he should be placed in class 5.



Name Roll Age ClassName
Kevin 4 9 5

Let's just assume, the roll number of Kevin be 4.


So, you being the Principal, all you need to do is, add the new student named Kevin to the existing List.


And how would you do that?


In the earlier tutorials we have seen that if we had requested student details, we got the response in JSON format.

Spring Boot - POST with @RequestBody in Rest API

Let us take the JSON response from the above image and format it properly :


[
	{"name":"John","roll":1,"age":8,"className":5},
	{"name":"Paul","roll":2,"age":7,"className":5},
	{"name":"Andrew","roll":3,"age":8,"className":5}
]

Now, in this case, we have to add the details of the student Kevin to the above JSON.


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

So, how to get it done?


Let us understand with the below steps :

  1. The first thing we have to do is, create a method named 'addStudent' 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.POST, value = "/students")
    	public void addStudent(@RequestBody Student student) {
    		studentService.addNewStudent(student);
    	}
    }

    Spring Boot - POST with @RequestBody in Rest API


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

    @RequestMapping(method = RequestMethod.POST, value = "/students")
    public void addStudent(@RequestBody Student student) {
    	studentService.addNewStudent(student);
    }


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

    @RequestMapping(method = RequestMethod.POST, value = "/students")


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

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


    And make a POST call to the /students endpoint.

  2. Next, to make a POST 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 POST.


      Spring Boot - POST with @RequestBody in Rest API

    2. Then type the endpoint to which you are going to make the POST call.



      localhost:8080/students

      Spring Boot - POST 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", "roll": 4, "age": 8, "className": 5}

      Spring Boot - POST with @RequestBody in Rest API

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


      Spring Boot - POST 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 - POST with @RequestBody in Rest API


    There is something @RequestBody Student student in its parameter.

    public void addStudent(@RequestBody Student student)


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

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


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


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


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


    And the addNewStudent 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)
    	));
    	public List studentDetails() {
    		return studentList;
    	}
    	public void addNewStudent(Student student) {
    		studentList.add(student);
    	}
    }
    
    The addNewStudent(...) method is called.
    
    public void addNewStudent(Student student) {
    	studentList.add(student);
    }

    Spring Boot - POST with @RequestBody in Rest API


    The details of the Student(i.e. Kevin) is added to the List studentList.

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

    Just note, we have changed the StudentService a little, which adds the details of the three existing students while declaring the List.

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

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


    Spring Boot - POST with @RequestBody in Rest API


    On hitting Send button, the details of Kevin is added to the endpoint localhost:8080/students.

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


    Spring Boot - POST with @RequestBody in Rest API


    And we can see that the details of Kevin is added along with John, Paul and Andrew.
    Spring Boot - POST with @RequestBody in Rest API