What is @RequestParam in Spring Boot?
@RequestParam is an annotation in Spring Boot used to retrieve request parameters from the URL and bind them directly to method parameters in a controller.
It is commonly used in Spring MVC and REST APIs to read query parameters sent by the client in HTTP requests.
In simple words, @RequestParam helps Spring Boot capture values from the URL
and use them inside controller methods.
Real-Time Example of @RequestParam
Consider the following URL:
http://localhost:8080/student?name=Naresh
Here:
student→ API endpointname→ request parameterNaresh→ parameter value
Spring Boot can read the value Naresh using @RequestParam.
Why is @RequestParam Used?
In web applications and REST APIs, clients frequently send data through query parameters.
Examples:
- Search keywords
- Pagination values
- Sorting options
- Filter conditions
- User input values
- Product IDs
Spring Boot uses @RequestParam to easily access these values.
Package of @RequestParam
import org.springframework.web.bind.annotation.RequestParam;
Basic Syntax of @RequestParam
@RequestParam("parameterName") datatype variableName
Simple Example of @RequestParam
@RestController
public class StudentController {
@GetMapping("/student")
public String getStudent(@RequestParam("name") String name) {
return "Student Name: " + name;
}
}
URL
http://localhost:8080/student?name=Naresh
Output
Student Name: Naresh
How @RequestParam Works Internally
When the client sends a request with query parameters:
- Spring Boot receives the HTTP request
- Spring MVC extracts query parameter values
- @RequestParam binds values to method arguments
- The controller method processes the values
- Response is returned to the client
Using Multiple Request Parameters
Multiple parameters can be captured using multiple @RequestParam annotations.
Example
@RestController
public class UserController {
@GetMapping("/user")
public String getUser(
@RequestParam("name") String name,
@RequestParam("age") int age) {
return "Name: " + name + ", Age: " + age;
}
}
URL
http://localhost:8080/user?name=Naresh&age=25
Output
Name: Naresh, Age: 25
Optional Parameters in @RequestParam
By default, request parameters are mandatory.
If a parameter is missing, Spring Boot throws an exception.
To make parameters optional:
@RequestParam(required = false)
Example of Optional Parameter
@RestController
public class EmployeeController {
@GetMapping("/employee")
public String getEmployee(
@RequestParam(required = false) String name) {
return "Employee Name: " + name;
}
}
URL Without Parameter
http://localhost:8080/employee
Output
Employee Name: null
Using Default Values in @RequestParam
Spring Boot allows default values using:
defaultValue = "value"
Example
@RestController
public class CourseController {
@GetMapping("/course")
public String getCourse(
@RequestParam(defaultValue = "Java") String name) {
return "Course Name: " + name;
}
}
URL Without Parameter
http://localhost:8080/course
Output
Course Name: Java
Using @RequestParam Without Parameter Name
If the variable name and parameter name are the same, parameter name can be omitted.
Example
@GetMapping("/hello")
public String hello(@RequestParam String name) {
return "Hello " + name;
}
URL
http://localhost:8080/hello?name=Naresh
Output
Hello Naresh
Using @RequestParam with Integer Values
@GetMapping("/square")
public int square(@RequestParam int number) {
return number * number;
}
URL
http://localhost:8080/square?number=5
Output
25
Using @RequestParam with Boolean Values
@GetMapping("/status")
public String status(@RequestParam boolean active) {
return "Status: " + active;
}
URL
http://localhost:8080/status?active=true
Output
Status: true
Using @RequestParam with List Values
Spring Boot can automatically bind multiple values into a list.
Example
@GetMapping("/skills")
public List<String> getSkills(
@RequestParam List<String> skill) {
return skill;
}
URL
http://localhost:8080/skills?skill=Java&skill=Spring&skill=MySQL
Output
[Java, Spring, MySQL]
Difference Between @RequestParam and @PathVariable
| Feature | @RequestParam | @PathVariable |
|---|---|---|
| Data Source | Query Parameters | URL Path |
| Example | ?id=101 | /student/101 |
| Optional Support | Easy | Limited |
| Best Use Case | Filtering and searching | Resource identification |
Difference Between @RequestParam and @RequestBody
| Feature | @RequestParam | @RequestBody |
|---|---|---|
| Data Location | URL Query Parameters | HTTP Request Body |
| Main Usage | Simple values | Complex JSON objects |
| HTTP Methods | Mainly GET | Mainly POST/PUT |
Advantages of @RequestParam
- Simple and easy to use
- Perfect for query parameter handling
- Supports optional parameters
- Supports default values
- Works with multiple data types
- Supports lists and collections
- Useful for filtering and pagination APIs
Disadvantages of @RequestParam
- Not suitable for large JSON objects
- Too many parameters can make URLs complex
- Sensitive data should not be passed in URLs
Real-Time Example in E-Commerce Application
In an e-commerce website, users may search products using:
http://localhost:8080/products?category=mobile&brand=samsung
Spring Boot controller:
@GetMapping("/products")
public String getProducts(
@RequestParam String category,
@RequestParam String brand) {
return "Category: " + category + ", Brand: " + brand;
}
Common Errors with @RequestParam
1. Missing Required Parameter
If a required parameter is not passed:
Required request parameter 'name' is not present
Solution:
- Use
required = false - Use
defaultValue
2. Type Mismatch Error
Example:
http://localhost:8080/square?number=abc
If integer is expected, Spring throws type conversion exception.
Best Practices for Using @RequestParam
- Use meaningful parameter names
- Use default values whenever appropriate
- Avoid passing sensitive data in URLs
- Use validation for numeric or mandatory values
- Use @RequestBody for large request payloads
- Keep URLs readable and SEO-friendly
Common Interview Questions on @RequestParam
What is @RequestParam in Spring Boot?
@RequestParam is used to retrieve query parameters from the URL and bind them to controller method parameters.
Is @RequestParam mandatory?
Yes, by default it is mandatory. But it can be made optional using:
@RequestParam(required = false)
Can @RequestParam have default values?
Yes. The defaultValue attribute can be used.
What is the difference between @RequestParam and @PathVariable?
@RequestParam reads query parameters, while @PathVariable reads values from the URL path.
Can @RequestParam handle multiple values?
Yes. It supports arrays and collections like List.
Conclusion
@RequestParam is one of the most important annotations in Spring Boot for handling query parameters in web applications and REST APIs.
It simplifies request handling, supports optional and default values, and works with multiple data types including lists and collections.
It is widely used in real-world applications for searching, filtering, pagination, sorting, and handling user input through URLs.
Understanding @RequestParam is essential for Spring Boot developers because it is frequently used in enterprise applications, REST APIs, microservices, and technical interviews.