Most Common Ways to Get Parameters in Spring Boot Controller - 5

Here we will describe mostly used ways to get parameters in the Spring Boot Controller. Given below  the list that is mostly used to get parameter values in spring boot API.

  1. Path Variables : @PathVariable
  2. Request Parameters : @RequestParam
  3. Servlet API : HttpServletRequest
  4. Request Body : @RequestBody
1. Path Variable: In the path variable, values are kept with the API path. Therefore we are to use /{variableName}/ to get this value with the API endpoint.

Spring boot get parameter value using path variable
Example

Spring boot get parameter value using path variable
Response of API
Response in a browser

2. Request Parameters: Request Parameters is the most useful way to get parameter in spring boot API.

Spring boot API get parameter using Request Parameter


3. HttpServletRequest: getParameter() method always gets parameter value as String.

Spring boot API get parameter using Http Servlet API


4. Request Body: Request Body is used to get complex parameters. Like, a table records get as JSON parameter in spring boot API.

Spring boot API get parameter using Request Body

Spring boot API response.
JSON Request send from Postman software

In Request Body we have used @PostMapping annotation. In the future blog, we will learn in-depth regarding @GetMapping, @PostMapping, and other annotations that mostly use the Spring Boot application.


Post a Comment

0 Comments