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

·Spring Boot 2.7Spring Boot 3.x
✅
Quick Understanding: @RequestParam is the Spring Boot annotation used to read query parameters and simple form values into controller method arguments. Use it for filtering, pagination, sorting, optional flags, and REST API inputs like `GET /products?category=electronics&page=0&size=20`.

🔍 What Is It?

@RequestParam binds HTTP request parameters to method arguments.

Sources it reads from:

  • →Query string: GET /products?category=electronics
  • →Form data: POST with application/x-www-form-urlencoded
  • →Multipart form: POST with multipart/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

DeclarationParameter absentParameter empty
@RequestParam(name = "q") String q400 Bad RequestEmpty string
@RequestParam(name = "q", required = false) String qnullEmpty string
@RequestParam(name = "q", defaultValue = "all") String q"all""all"
@RequestParam(name = "page", defaultValue = "0") int page00

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.

java
import java.util.List;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class RequestParamDemoController {
    @GetMapping("/hello")
    public String hello(
            @RequestParam(name = "name", defaultValue = "World") String name) {
        return "Hello, " + name;
    }

    @GetMapping("/tags")
    public List<String> tags(@RequestParam(name = "tag") List<String> tags) {
        return tags;
    }
}

// GET /hello                  -> Hello, World
// GET /hello?name=Sam          -> Hello, Sam
// GET /hello?name=             -> Hello, World
// GET /tags?tag=spring&tag=java -> ["spring", "java"]

🤔 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

Senior Architect's Perspective

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

How do I bind multiple request params to an object?

Use @ModelAttribute: @GetMapping with @ModelAttribute ProductFilter filter. Spring binds all matching query params to the filter object's fields. The filter class needs setters or a matching constructor. This is cleaner than 10 individual @RequestParam annotations.

What is the difference between @RequestParam and @PathVariable?

@PathVariable is for values that identify a resource in the URL path like `/users/{id}`, while @RequestParam is for query-string values that filter, sort, paginate, search, or slightly change how a request is handled like `/users?role=ADMIN&page=0`.

Can @RequestParam be optional in Spring Boot?

Yes. Use `required = false` when the value can be absent, or use `defaultValue` when you want Spring to supply a fallback automatically.

Does defaultValue handle an empty request parameter?

Yes. In the standard Spring MVC resolver, defaultValue is used when the parameter is missing or its value is empty. Supplying defaultValue also makes the parameter optional. It does not recover from invalid type conversion such as page=abc for an int.

What is the default value of required in @RequestParam?

required defaults to true. A missing required parameter normally produces a 400 response. Use required=false, an Optional argument, or a defaultValue when the endpoint permits absence.

How do I receive repeated query parameters as a list?

Declare @RequestParam(name="tag") List<String> tags and send repeated names, for example ?tag=spring&tag=java. Use an unnamed MultiValueMap argument when you need repeated values for several parameter names.

Does @RequestParam read a JSON request body?

No. Use @RequestBody for a JSON payload. In Spring MVC, @RequestParam binds Servlet request parameters such as query values and form fields. In Spring WebFlux it binds query parameters; form and multipart handling differ.

🎯 Key Takeaways

1@RequestParam extracts query string parameters (?key=value)
2required=false makes a param optional; defaultValue provides a fallback
3Spring auto-converts String params to int, boolean, enum, LocalDate, etc.
4Use List<String> for multi-value params (?tag=spring&tag=java)
5@ModelAttribute binds multiple params to an object at once
6Always paginate list endpoints — never return unbounded collections

🎯 Practice this topic in interviews

↗ Where this topic fits

Sources

Sources / References

Use these Spring Boot references to verify the concept in official docs and continue into the most relevant SplashRide pages.

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.