Spring boot @RequestParam annotation Example

Hey guys in this post we will discuss Spring boot @RequestParam annotation and its example

Read Spring boot @PathVariable annotation


The @RequestParam annotation binds the web request parameter to a controller method. In other words, @RequestParam annotation is used to obtain a parameter from the URI


It has the following optional elements:

  • defaultValue – used as a fallback when the request parameter is not provided or has an empty value
  • name – the name of the request parameter to bind to
  • required – tells whether the parameter is required
  • value – alias for name

Watch the video

Example on @RequestParam

Now let’s look at the development steps for the @RequestParam annotation

Create spring boot project

There are different ways to create a spring boot project, you can check out the below articles to create a spring boot project.

>> Create spring boot application using Spring initializer
>> Create spring boot application in Spring tool suite [STS]
>> Create spring boot application in IntelliJ IDEA

Add the maven dependencies

Add the following maven dependencies to pom.xml and update the project

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
		<relativePath/> <!-- lookup parent from repository -->
	<description>Spring boot path variable</description>




spring-boot-starter-web dependency for building web applications using Spring MVC. It uses the tomcat as the default embedded container.

spring-boot-devtools dependency for automatic reloads or live reload of application.

Create controller

Create UserController.java class inside the in.bushansirgur.requestparam package and add the following content

package in.bushansirgur.requestparam.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

public class UserController {
	public String getName(@RequestParam(name = "userName") String name) {
		return "My name is "+name;
	public String getCustomerName(@RequestParam(defaultValue = "anonymous") String name) {
		return "My name is "+name;
	public String getEmployeeName(@RequestParam(required = false) String name) {
		return "My name is "+name;

Run the project

To run the project, execute the following maven command

mvn sprint-boot:run

Here the URI template http://localhost:8080/users mapped to getName() method. @RequestParam(name = "userName") annotation gets the request parameter from the URI and binds it to the method variable name.

User can pass any number of request parameters in the URI, each parameter is separated with &


The URI template http://localhost:8080/customers mapped to getCustomerName(). If the request parameter name is the same as the method variable name then we don’t need to specify the name option in the annotation. If we don’t pass the parameter in the request URI then the defaultValue gets assigned to the method variable name.


The URI template http://localhost:8080/employees mapped to getEmployeeName(). By default @RequestParam the required option is true, if you don’t send parameters in the request URI then we will get an error. To avoid that we can set the required option to false, it will not throw any error if we don’t send parameters in the request URI.


Bushan Sirgur

Hey guys, I am Bushan Sirgur from Banglore, India. Currently, I am working as an Associate project in an IT company.

Leave a Reply