Hey guys in this post we will discuss Spring boot @RequestParam
annotation and its example
Read Spring boot
@PathVariable
annotation
Table of Contents
Overview
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
http://localhost:8080/springmvc/hello/101?param1=10¶m2=20
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">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.2</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>in.bushansirgur</groupId>
<artifactId>pathvariable</artifactId>
<version>v1</version>
<packaging>war</packaging>
<name>pathvariable</name>
<description>Spring boot path variable</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
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;
@RestController
public class UserController {
@GetMapping("/users")
public String getName(@RequestParam(name = "userName") String name) {
return "My name is "+name;
}
@GetMapping("/customers")
public String getCustomerName(@RequestParam(defaultValue = "anonymous") String name) {
return "My name is "+name;
}
@GetMapping("/employees")
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.