Learn how to use @RequestParam in Spring Boot to handle query parameters effectively with practical examples from real-world scenarios like search, filtering, and pagination.
What is @RequestParam
@RequestParam is a Spring Boot annotation used to bind query parameters from the URL to method parameters in a controller. Unlike @PathVariable, which is used for identifying resources, @RequestParam is mainly used for filtering, searching, and optional data inputs.
@RequestParamis used in Spring Boot to extract query parameters from the URL and bind them to method parameters for filtering, searching, or optional inputs in REST APIs.
For example, /products?category=books&price=500 uses query parameters to filter results. It is widely used in real-world applications like e-commerce for search functionality, pagination, and sorting. It also supports optional values, default values, and multiple inputs. This makes APIs more flexible without changing the endpoint structure, improving usability and scalability in production systems.
In Spring Boot, @RequestParam is used to extract values from query parameters in the URL. It is commonly used when you want to pass optional or filtering data to your APIs without changing the URL structure.
Let’s start with a simple example:
@GetMapping("/users")
public String getUserById(@RequestParam Long userId) {
return "User ID: " + userId;
}
Use our Online Code Editor
When a request like /users?userId=101 is made, Spring maps 101 to the userId parameter.
Now consider a real-world e-commerce scenario where users search for products:
@GetMapping("/products")
public String searchProducts(@RequestParam String category,
@RequestParam String sortBy) {
return "Category: " + category + ", Sort By: " + sortBy;
}
Use our Online Code Editor
Example request: /products?category=electronics&sortBy=price
You can also make parameters optional:
@GetMapping("/products")
public String getProducts(@RequestParam(required = false) String category) {
return "Category: " + category;
}
Use our Online Code Editor
Or provide default values:
@GetMapping("/products")
public String getProducts(
@RequestParam(defaultValue = "all") String category) {
return "Category: " + category;
}
Use our Online Code Editor
For multiple values:
@GetMapping("/products/filter")
public String filterProducts(@RequestParam List<String> tags) {
return "Tags: " + tags;
}
Use our Online Code Editor
In real-world systems like CRM or booking platforms, @RequestParam is used for filtering data such as date ranges, status, pagination (page, size), or sorting options.
Best practices include keeping query parameters meaningful, using default values for better UX, validating inputs, and avoiding too many parameters in a single API.
Using @RequestParam properly helps build flexible, scalable, and user-friendly APIs aligned with REST standards.
Have a great one!!!
Author: Ayush Shrivastava
Thank you for being a part of the community
Before you go:
Whenever you’re ready
There are 4 ways we can help you become a great backend engineer:
- The MB Platform: Join thousands of backend engineers learning backend engineering. Build real-world backend projects, learn from expert-vetted courses and roadmaps, track your learning and set schedules, and solve backend engineering tasks, exercises, and challenges.
- The MB Academy: The “MB Academy” is a 6-month intensive Advanced Backend Engineering Boot Camp to produce great backend engineers.
- Join Backend Weekly: If you like posts like this, you will absolutely enjoy our exclusive weekly newsletter, sharing exclusive backend engineering resources to help you become a great Backend Engineer.
- Get Backend Jobs: Find over 2,000+ Tailored International Remote Backend Jobs or Reach 50,000+ backend engineers on the #1 Backend Engineering Job Board.

Top comments (0)