Spring Boot @RequestParam Annotation Explained with Real-Time Examples

 Introduction :

As a Java Full Stack Developer, I have worked on several Spring Boot applications where APIs needed to receive data from users. One annotation that I frequently used in REST APIs is @RequestParam.

In many real-world applications, users need to search, filter, sort, or paginate data. For example, an employee management system may allow users to search employees by department, status, or employee ID. In such cases, @RequestParam provides a simple and efficient way to pass data from the client to the server through URL query parameters.

When I started learning Spring Boot, I often got confused between @RequestParam, @PathVariable, and @RequestBody. However, after working on multiple projects, I understood the purpose of each annotation and when to use them effectively.

In this article, I will explain the @RequestParam annotation with practical examples, project use cases, common mistakes, best practices, and interview questions.


What is @RequestParam?

The @RequestParam annotation is used to extract query parameters from an HTTP request and bind them to method parameters in a Spring Boot controller.

For example:

http://localhost:8080/user?name=Sulochana

Here, the value of name can be accessed using @RequestParam.


Syntax

@RestController
public class UserController {

    @GetMapping("/user")
    public String getUser(@RequestParam String name) {
        return "Hello " + name;
    }
}

Request

GET /user?name=Sulochana

Response

Hello Sulochana

Where I Used @RequestParam in Projects

In one of my projects, users needed the ability to filter records based on different criteria.

Examples:

GET /employees?department=IT

GET /employees?status=ACTIVE

GET /employees?page=1&size=10

Using @RequestParam, I was able to capture these values and apply filtering and pagination logic in the backend.

This approach made the APIs simple, clean, and easy to test using Postman.


Required Request Parameter

By default, request parameters are mandatory.

@GetMapping("/employee")
public String getEmployee(@RequestParam String empId) {
    return "Employee ID: " + empId;
}

Request

GET /employee?empId=101

Response

Employee ID: 101

If the parameter is not provided, Spring Boot throws an exception.


Optional Request Parameter

Sometimes users may not provide all parameters.

In such cases, we can make them optional.

@GetMapping("/welcome")
public String welcome(
        @RequestParam(required = false) String name) {

    return "Welcome " + name;
}

Request

GET /welcome

Response

Welcome null

Default Value Example

To avoid null values, we can define a default value.

@GetMapping("/wish")
public String greet(
        @RequestParam(defaultValue = "Guest") String name) {

    return "Hello " + name;
}

Request

GET /wish

Response

Hello Guest

Multiple Request Parameters

Multiple values can be accepted in a single request.

@GetMapping("/student")
public String studentDetails(
        @RequestParam String name,
        @RequestParam int age) {

    return "Name: " + name + ", Age: " + age;
}

Request

GET /student?name=Seshu&age=35

Response

Name: Seshu, Age: 35

Using @RequestParam for Pagination

One of the most common use cases I encountered in projects was pagination.

@GetMapping("/employees")
public String getEmployees(
        @RequestParam int page,
        @RequestParam int size) {

    return "Page: " + page + ", Size: " + size;
}

Request

GET /employees?page=1&size=10

This helps applications load data efficiently instead of fetching all records at once.


@RequestParam with List

Spring Boot can automatically bind multiple values to a list.

@GetMapping("/courses")
public List<String> getCourses(
        @RequestParam List<String> course) {

    return course;
}

Request

GET /courses?course=Java&course=SpringBoot&course=React

Response

[
  "Java",
  "SpringBoot",
  "React"
]

Common Mistake I Faced

During the early stages of my career, I tried passing too many values using @RequestParam.

Example:

/user?name=Sulochana&age=32&city=Hyderabad&department=IT&designation=Developer

As the number of parameters increased, the URL became difficult to maintain.

Later, I learned that complex data should be sent using @RequestBody as JSON.

This made the API cleaner and easier to manage.


Common Errors :

Missing Required Parameter

@RequestParam String name

Request:

GET /user

Error:

Required request parameter 'name' is not present

Solution

@RequestParam(required = false)

or

@RequestParam(defaultValue = "Guest")

Type Conversion Error

@RequestParam int age

Request:

/user?age=abc

Error:

Failed to convert value of type String to Integer

Always validate user input before processing.


Best Practices

Based on my experience, I recommend the following:

  • Use @RequestParam for simple values.

  • Use meaningful parameter names.

  • Use default values when appropriate.

  • Avoid sending large amounts of data through query parameters.

  • Use @RequestBody for complex objects.

  • Validate request parameters before processing.


Interview Questions

1. What is @RequestParam in Spring Boot?

It is used to bind query parameters from an HTTP request to controller method parameters.

2. Is @RequestParam required by default?

Yes, it is required by default.

3. How can you make a request parameter optional?

@RequestParam(required = false)

4. How do you provide a default value?

@RequestParam(defaultValue = "Guest")

5. What is the difference between @RequestParam and @PathVariable?

@RequestParam retrieves query parameters, while @PathVariable retrieves values from the URL path.

6. When should you use @RequestBody instead of @RequestParam?

Use @RequestBody when handling complex JSON objects.


Conclusion :

The @RequestParam annotation is one of the most commonly used annotations in Spring Boot REST APIs. Throughout my development experience, I have used it extensively for search, filtering, sorting, and pagination functionalities.

By understanding how to use required parameters, optional parameters, default values, and multiple request parameters, developers can build clean and efficient APIs.

I hope this article helped you understand the practical usage of @RequestParam in Spring Boot.

In the next article, we will explore the @PathVariable annotation and understand how it differs from @RequestParam. We will discuss real-world use cases, examples, common mistakes, and interview questions to help you master another important Spring Boot annotation.

Comments

Popular posts from this blog

React Basics – How to Create Component

How to Build REST API using Spring Boot (Step-by-Step Guide)