Spring @RequestParam Annotation Example
In this section we will learn about @RequestParam Annotation.
The @RequestParam annotation enables spring to extract input data that may be passed as a query, form data, or any arbitrary custom data.
If we break the above URL down into smaller parts, then we will see that:
- HTTP – is the protocol being used,
- localhost – is the domain name,
- 8080 – is the port number,
- api – is the root path of your web services application,
- users – is most likely the @Path value of your Root Resource class and,
- name – is the URL Query parameter which we can read with @RequestParam annotation,
- email– is the URL Query parameter which we can also read with @RequestParam annotation.
The following example creates a Spring Boot web application which uses @RequestParam annotation.
Project Directory
Pom.xml
<?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>3.0.1</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.knf.dev.demo</groupId>
<artifactId>spring-requestparam-example</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring-requestparam-example</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>17</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-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>
User.java
package com.knf.dev.demo.dto;
public class User {
private String name;
private String country;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
public User(String name, String country) {
this.name = name;
this.country = country;
}
}
UserController.java
package com.knf.dev.demo.controller;
import com.knf.dev.demo.dto.User;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("/api/v1")
public class UserController {
@GetMapping(value = "/users")
public User getUserByCountryAndName(
@RequestParam(value = "name") String name,
@RequestParam String country) {
return new User(name,country);
}
}
Run Application - Application.java
Application is the entry point that sets up the Spring Boot application. The @SpringBootApplication annotation enables auto-configuration and component scanning.
Let's run this Spring boot application from either IntelliJ IDEA IDE by right click - Run 'Application.main()'
Or you can use the below maven command to run:
mvn spring-boot:run
Try to send a GET request to the /users?country=India&name=Sibin
endpoint using Postman.