Handling Request Parameters

It is important to handle request parameters in right way. There are different ways in which this can be done. Below are the ways in which we can handle request parameters.

  • @RequestParam annotation: It is used to bind the method parameters to web request parameters
  • @RequestBody annotation: It is used to bind the method parameter to the body of the web request
  • @PathVariable annotation: It is used to bind the method parameter to the URI template variable

1. @RequestParam

Query parameters from the url can be mapped using @RequestParam annotation. such as

http://localhost:8080/registerUser?name=Rohit Kumar&password=changeit&email=r@gmail.com

From a microservice perspective use the below code and test it using postman

package com.sks.controller;

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class UserController {
	
	//http://localhost:8080/user/registerUser?name=Rohit Kumar&password=changeit&email=r@gmail.com
	@PostMapping("/registerUser")
	public String registerUser(@RequestParam("name") String name, @RequestParam("password")String password, 
			@RequestParam("email")String email) {
		
		return "The user has been registered successfully. Below are the details: " +
		     "User name : " + name + ", password : " + password + ", email : " + email;
		
	}

}

Test using postman

2. @RequestBody

In this section, we will learn when and how to use the RequestBody annotation (@RequestBody) for handling web requests.

When the request parameters are sent as part of body of the request, RequestBody annotation is used to bind the method parameter with the body of the incoming request.

The RequestBody annotation is most commonly used in scenarios dealing with REST APIs where request is sent mostly as a json request. 

The following example explains the usage of the @RequestBody annotation:

{
    "name":"Rohit Kumar",
    "password":"changeit",
    "email":"r@gmail.com",
    "age":34
}

The above json request will be mapped to User object using @RequestBody annotation as shown below.

public class User {

	private String name;
	private String password;
	private String email;
	private int age;

        //constructors, getter/setters, toString()

}
@RestController
public class UserController {

	@PostMapping("/register")
	public String register(@RequestBody User user) {
		
		return "The user has been registered successfully. Below are the details: " +
		     user;
		
	}
}

Output :

3. @PathVariable

The PathVariable annotation is used to bind a method parameter to a URI template variable. When request parameter is sent as path variable, it can be bound with method parameter using @PathVariable annotation. A URI template is a URI-like string containing one or more variables. For example, in  the following code, /{name} is a path variable. A method can have multiple @Pathvariable annotations to bind multiple variables present in the URI template.

In the below example, userName will be mapped from the url path. http://localhost:8080/display/Manish

Value of userName would be ‘Manish’ in this case.

@RestController
public class UserController {

	@GetMapping("/display/{name}")
	public String displayNameInCapital(@PathVariable String name) {
		return "Hello, " + name.toUpperCase();
	}

}

Ouptut: