Java Spring Boot — More About REST Controller : Adding Request Method, Request Param, Request Body, and Path Variable
In this tutorial, I will show you how to configure the Request Method, Request Param, Request Body, and Path Variable to our REST Controller. Let’s begin!
Adding Request Method
First, to demonstrate what we will do and to make sure it will work properly, I create a List of Student object named students in the Application class. Notice that I make the List as a public and a static object so we can access it directly without making an object from the Application class in another class. For the initiation, I add two student object to the List.
Then I change the getAllStudent() function as below which will return the List of student object from the Application class we created before. Also I add the value and the method parameter to the @RequestMapping annotation. The value parameter specifies the URL path of the corresponding function we want to call via REST API, while the method parameter specifies what type of REST method we want to use for this function For this function, we want to use it as a GET method.
When we run again the .jar file, we will see the following return. As we can see, we get the List of student object as we initiate in the Application class when we call the http://localhost:8080/students URL as we specify in the @RequestMapping annotation by the value parameter.
To see what is the impact of using the method parameter in the @RequestMapping annotation, we will try to call the URL again using postman. Look that if we call the URL and set the method as GET method, the request will success with the expected result since we define method = RequestMethod.GET in the @RequestMapping annotation.
But when we try again to call the URL using different method, let say using POST method, we will see the following error as Spring Boot doesn’t allow another method except GET method.
Adding Request Param
To add the request param to our REST API URL path, we use @RequestParam annotation before we specify the parameter of our function. I create a function named addStudent which accept two parameters, studentNumber and name.
We want to use this function to add a Student object to the List, so I add the following code to the function.
Pay attention that I add a new parameter to @RequestMapping annotation called produces with the value “application/json”. This parameter makes sure that the return of the function will be in JSON format.
Now let’s try to run our app again and call the addStudent function using postman. This time we set the method as POST method and specify the request param.
If we check again the List by calling the getAllStudents function, we will see that the new student object has been added to the List.
Adding Request Body
Let’s try to configure our REST API so it can receive a request body in JSON format. I created a new function named editFirstStudent which receives a Student object as its parameter. I add a @RequestBody annotation before the declaration of the parameter so Spring Boot will know that the function will receive the parameter as a request body in JSON format. I will try to make this function to edit the first element of the List.
Now run again the app and we call the corresponding URL as well as adding the Body in the request.
We will get the following response. Notice that the request has been succeeded as the first element of the List is already changed.
Adding Path Variable
The last section is to add path variable into our REST API. I create a function that receives an integer parameter named index. Before the declaration of the parameter, I add a @PathVariable annotation so the Spring Boot will know that this parameter will be obtained from the URL path as I defined in the @RequestMapping annotation. I make this function to delete an element from the List. The index which will be deleted is specified at the URL path.
Now let’s run again the app and try to add 0 to the URL path so it will delete the first element of the List. Should we succeed, we will get the following response.
Here is the repository of this project :