Binding a list in RequestParam
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Spring MVC can bind request parameters directly into a List when the incoming values are simple types such as strings, integers, or longs. The two most common input shapes are repeated parameters and comma-separated values. Both can be convenient, but you should choose the one that matches how your clients actually send requests.
Basic List Binding
A controller can bind a list parameter directly.
This works with requests like:
- '
/products?ids=1,2,3' - '
/products?ids=1&ids=2&ids=3'
Spring converts the incoming values into the target element type, here Long.
Comma-Separated Versus Repeated Parameters
Both forms are widely used, but they are not identical from an API-design point of view.
Comma-separated form:
Repeated-parameter form:
Repeated parameters are often clearer because they avoid extra splitting rules in clients and proxies. Comma-separated values are concise and still common. Spring can usually handle either for simple scalar types.
Optional Lists
If the parameter is optional, mark it as not required.
This avoids a missing-parameter error when the client does not send the list at all.
Be explicit about what absence means in your API. Sometimes an omitted list means "no filter," while an empty list means "filter to nothing." Those are not always the same business rule.
Type Conversion and Validation
Simple element types work well because Spring can convert them automatically.
Examples that bind cleanly:
- '
List<String>' - '
List<Integer>' - '
List<Long>' - '
List<UUID>if conversion support is present'
If one value cannot be converted, Spring throws a binding error. That means /products?ids=1,two,3 will fail for List<Long>.
This is usually what you want because the request is malformed.
When @RequestParam Is the Wrong Tool
@RequestParam is best for simple lists in query strings or form parameters. It is not the right choice for complex nested structures.
If the client is sending a JSON array in the request body, use @RequestBody instead.
Likewise, if the input is a richer form object with several fields, a request DTO or @ModelAttribute is usually cleaner than many independent request parameters.
Testing the Binding Behavior
If you are unsure how your controller will interpret a query string, write a small MockMvc test. That is especially useful when several clients call the same endpoint and you want to lock in one accepted format.
This kind of test documents the contract more clearly than prose alone. If you also accept comma-separated input, add a second test so future refactoring does not accidentally narrow the supported format.
Common Pitfalls
The most common mistake is using @RequestParam for complex structured data that really belongs in a request body object.
Another mistake is assuming every client will send the list in the same format without documenting whether the API expects repeated parameters or comma-separated values.
A third pitfall is forgetting that missing and empty list semantics may need different business handling.
Summary
- Spring MVC can bind request parameters directly into
Listtypes. - Repeated parameters and comma-separated values are both common input formats.
- Use
required = falsewhen the list is optional. - Simple scalar element types bind cleanly; malformed values trigger conversion errors.
- Use
@RequestBodyinstead of@RequestParamwhen the client is sending structured JSON data.

