@RequestParam is an annotation used to pass the query parameter in Spring. This quick tutorial will introduce you to Spring @RequestParam and it's attributes - name, required, value and defaultValue. We'll discuss when to use each one and show a lot of code examples on this subject. We'll also discuss when it's better to use @RequestParam over @PathVariable.
Passing query parameter in Spring and @RequestParam annotation
So, what is @RequestParam annotation? It's the way to pass variables in Spring request using a query approach. For example, let's discuss the following code example.
@GetMapping("/api/users")
@ResponseBody
public String getUserByEmail(@RequestParam String email) {
return "Provided email is: " + email;
}
What we have here is a description of a service that processes GET requests. It should return the value of type String that contains "name" - provided as @RequestParam. This means that when you want to pass the parameter this way, you'll need to add ?paramName=paramValue on the end of the URL string.
If we call this service with a request containing "johndoe@gmail.com" as a value of email parameter we'll get the following result:
https://localhost:3030/api/users?email=johndoe@gmail.com
----
Provided email is: johndoe@gmail.com
You can also add the following parameters to the @RequestParam annotation:
- name
- value
- required
- defaultValue
Name parameter
It represents the name of the query variable to be provided to service. When you don't have a name parameter, the default expected name for @RequestParam will be the name of the controller method variable. You'll usually use the name parameter when you want to name @RequestParam differently than your method variable. It's not a problem even if they are called the same, but then you have unnecessary code. This might sound confusing, but let's check it out on example:
@GetMapping("/api/users")
@ResponseBody
public String getUserByEmail(@RequestParam (name="emailAddress") String email) {
return "Provided email is: " + email;
}
So, what we see here is that in this request @RequestParam is named emailAddress, but the method variable is called email. We will invoke service this way:
https://localhost:3030/api/users?emailAddress=johndoe@gmail.com
----
Provided email is: johndoe@gmail.com
Value parameter
The value parameter is used for the same purpose as name parameter, it's a name parameter alias. You can use one of them, and if you like - even both, but always give them the same value. Otherways your application will have strange, unpredicted behavior concerning that @RequestParam.
@GetMapping("/api/users")
@ResponseBody
public String getUserByEmail(@RequestParam (name="emailAddress", value="emailAddress") String email) {
return "Provided email is: " + email;
}
This way you'll have the following response:
https://localhost:3030/api/users?emailAddress=johndoe@gmail.com
----
Provided email is: johndoe@gmail.com
Required parameter
Method variables annotated with @RequestParam are required by default. This means we will get an error if the parameter isn’t present in the request. To enable providing null parameter values you should put the required parameter to false.
@GetMapping("/api/users")
@ResponseBody
public String getUserByEmail(@RequestParam (required=false) String email) {
return "Provided email is: " + email;
}
And when you invoke this service, you'll get:
https://localhost:3030/api/users
----
Provided email is: null
You can also pass null @RequestParam values if you're using Java 8 Optional, without even providing the required keyword:
@GetMapping("/api/users")
@ResponseBody
public String getUserByEmail(@RequestParam Optional <String> email) {
return "Provided email is : " + email.orElseGet(() -> null);
}
DefaultValue parameter
The defaultValue parameter is used in situations when you want to cover the situation where the user does not provide @RequestParam in a request. So, even if you didn't set required to false, this request will not throw an error. Instead, the @RequestParam value will be replaced with defaultValue.
@GetMapping("/api/users")
@ResponseBody
public String getUserByEmail(@RequestParam (defaultValue="noEmail") String email) {
return "Provided email is : " + email;
}
The response will be:
https://localhost:3030/api/users
----
Provided email is: null
That's it about Spring @RequestParam. Happy coding!