@RequestParam List — Multi-Value Query Parameters in Spring
Fast recall
Quick revision
Core answer
Knowledge of multi-value query parameter binding in Spring MVC — shows understanding beyond single-value parameters
Not knowing @RequestParam can bind to List or array — Learners try to manually parse a comma-separated string
Demonstrate both repeated parameter style and comma-separated style with actual code examples showing the difference
Interview-ready guide
Interview answer
@GetMapping("/items") List
@RequestParam List — Multi-Value Query Parameters in Spring
Direct Answer
@RequestParam List<String> ids can collect repeated values such as /items?ids=1&ids=2&ids=3. With Spring's normal conversion service, comma-delimited input can also be converted for suitable collection element types, but conversion rules/custom converters can change this. The parameter is required by default; use defaultValue, Optional, or required=false when absence has a defined meaning.
Code Example
@RestController
@RequestMapping("/api/products")
public class ProductController {
private final ProductService productService;
@GetMapping
public List<Product> getProducts(
@RequestParam(required = false) List<String> categories,
@RequestParam(required = false) List<Long> ids) {
if (ids != null && !ids.isEmpty()) {
return productService.findByIds(ids); // batch fetch
}
if (categories != null && !categories.isEmpty()) {
return productService.findByCategories(categories); // multi-filter
}
return productService.findAll();
}
@DeleteMapping("/batch")
public void deleteProducts(@RequestParam List<Long> ids) {
productService.deleteByIds(ids); // bulk delete
}
@GetMapping("/tags")
public List<Product> getByTags(@RequestParam("tag") String[] tags) {
return productService.findByTags(List.of(tags)); // array alternative
}
@GetMapping("/unique-categories")
public List<Product> getByUniqueCategories(@RequestParam Set<String> categories) {
return productService.findByCategories(categories); // Set removes duplicates
}
}
Common Mistakes & Pitfalls
- Manually parsing comma-separated strings: Freshers often declare
@RequestParam String idsthen split it withids.split(","). Spring can bind to List directly — no manual parsing needed. Just use@RequestParam List<String> idsand it handles both repeated params and comma-separated values. - Using primitive arrays: Writing
@RequestParam long[] idscauses type conversion issues. Use boxed types:@RequestParam List<Long> idsor@RequestParam Long[] ids. Spring's conversion works better with wrapper classes. - Forgetting required=false for optional lists: If the parameter is optional, you must set
@RequestParam(required = false) List<String> categories, otherwise Spring throws an exception when the param is missing. Then check for null or empty in your code before using the list.
Check your understanding
Answer these without looking back at the article.
- What is the difference between repeated params and comma-separated params in URLs?
- How do you handle an empty list when @RequestParam List is required=false?
- What is MultiValueMap in Spring MVC?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.