Spring @RequestParam
Learn how @RequestParam works in Spring Boot with query parameters, optional params, default values, list filters, form binding, and REST API examples.
Last reviewed: November 2026
🔍 What Is It?
@RequestParam binds HTTP request parameters to method arguments.
Sources it reads from:
- →Query string:
GET /products?category=electronics - →Form data:
POSTwithapplication/x-www-form-urlencoded - →Multipart form:
POSTwithmultipart/form-data
Key attributes:
- →
name/value— parameter name; specify it explicitly to avoid relying on compiler parameter-name metadata - →
required— default true; false makes it optional - →
defaultValue— used when the parameter is absent or empty; also makes it optional
Required, optional, and default values
| Declaration | Parameter absent | Parameter empty |
|---|---|---|
@RequestParam(name = "q") String q | 400 Bad Request | Empty string |
@RequestParam(name = "q", required = false) String q | null | Empty string |
@RequestParam(name = "q", defaultValue = "all") String q | "all" | "all" |
@RequestParam(name = "page", defaultValue = "0") int page | 0 | 0 |
These examples assume Spring MVC's standard binding configuration. A custom converter can change how empty text is handled. An invalid integer such as ?page=abc fails conversion; defaultValue does not replace malformed values. For an optional numeric parameter without a default, use Integer or Optional<Integer> rather than a primitive int.
Supported types:
- →Primitives:
int,long,boolean - →Wrappers:
Integer,Long,Boolean - →
String,Date,LocalDate - →Enums (by name)
- →
List<String>for multi-value params - →
MultiValueMap<String, String>for all params
🎯 Why Do We Need It?
Query parameters are the standard way to pass filtering, sorting, pagination, search text, and optional flags in Spring Boot REST APIs. @RequestParam gives you type-safe binding, automatic conversion, validation, and default values, which is much cleaner and safer than manually reading HttpServletRequest.getParameter().
🏗️ What Happens in Real Projects?
Used in every list/search endpoint:
- →
GET /products?category=electronics&minPrice=100&maxPrice=500&page=0&size=20&sort=price,asc - →
GET /orders?status=PENDING&customerId=123&from=2024-01-01&to=2024-12-31 - →
GET /users?search=john&role=ADMIN&active=true
⚙️ How It Works
Spring's argument resolvers read request parameters and convert them to the declared Java type through ConversionService. That means "42" becomes int 42, "true" becomes boolean true, "2026-08-27" can become LocalDate, and enum strings like "ACTIVE" can bind to Status.ACTIVE when the type is declared correctly.
💻 Example
Request Parameters in Practice — Filtering, pagination, and optional parameters.
🤔 Common Confusions
❓ @RequestParam vs @PathVariable — when to use which?
@PathVariable is for resource identifiers in the URL path: /users/{id}. @RequestParam is for optional filtering, sorting, pagination in the query string: /users?role=ADMIN&page=0. Rule of thumb: if it identifies a resource, use path variable. If it modifies how you retrieve/filter a resource, use request param.
❓ What happens if a required @RequestParam is missing?
Spring throws MissingServletRequestParameterException, which results in a 400 Bad Request. This is the correct behavior. Make params optional with required=false or provide defaultValue when absence is valid.
🚨 Common Production Issues
No pagination on list endpoints — add @RequestParam page/size with defaults and enforce max page size
Integer overflow from large page param values — validate page/size ranges
SQL injection via @RequestParam passed directly to queries — always use parameterized queries
✅ Best Practices
Always provide defaultValue for page/size parameters
Enforce max page size to prevent OOM: if (size > 100) size = 100
Use @Validated on the controller and @Max/@Min on params for automatic validation
Document all query parameters in your API documentation (Swagger/OpenAPI)
🏛️ Architect's Note
For complex search/filter APIs, consider creating a dedicated search criteria object and using @ModelAttribute to bind all params at once. Or use Spring Data JPA Specifications or QueryDSL for dynamic queries. Avoid building query strings manually from @RequestParam values — it's fragile and SQL-injection-prone.
💬 Frequently Asked Questions
🎯 Key Takeaways
🔗 Related Topics
🎯 Practice this topic in interviews
↗ Where this topic fits
Spring Boot Technology Guide
Return to the Spring Boot learning hub.
Backend Engineer Career Path
A structured journey through Core Java, Spring Boot, production backend engineering, and interview preparation.
Full Stack Java Developer Career Path
A guided route through frontend foundations, Java backend engineering, delivery workflows, and interview readiness.
Sources
Sources / References
Use these Spring Boot references to verify the concept in official docs and continue into the most relevant SplashRide pages.
Official documentation
Related SplashRide pages
SplashRide Spring Boot learning path
Browse the full Spring Boot topic collection and learning order on SplashRide.
Spring Boot Interview Prep
Practice Spring Boot interview questions and scenario-based answers.
Backend Engineer Career Path
A structured journey through Core Java, Spring Boot, production backend engineering, and interview preparation.
Full Stack Java Developer Career Path
A guided route through frontend foundations, Java backend engineering, delivery workflows, and interview readiness.
Controllers
Master Spring MVC Controllers — the web layer that handles HTTP requests, maps URLs to methods, and returns responses in enterprise REST APIs.
Request Mapping
Master Spring MVC request mapping - how to map HTTP requests to controller methods using @RequestMapping, @GetMapping, @PostMapping, and the full range of mapping options.