@RequestParam in Spring MVC — Query String Parameter Binding
Fast recall
Quick revision
Core answer
Understanding of query string parameter binding, which is essential for building list and search endpoints in real applications
Making all @RequestParam fields required without default values, causing 400 errors when clients omit optional filtering parameters
Show a realistic pagination or filtering example with required=false and defaultValue, demonstrating understanding of optional API design
Interview-ready guide
Interview answer
@RequestParam(defaultValue = "0") int page and @RequestParam(required = false) Status status provide a usable default page and optional filter. If a required parameter is missing, MVC normally raises MissingServletRequestParameterException. If a supplied number is malformed, conversion fails. A default should reflect the endpoint contract, not be added only to avoid errors.
@RequestParam in Spring MVC — Query String Parameter Binding
Direct Answer
@RequestParam binds request parameters, commonly from the query string, to a handler argument. It is required by default unless a default value, Optional, or required=false changes that contract. Use query parameters for filters, search, sorting, pagination, and other request options; they can also appear in form-encoded requests.
Code Example
@RestController
@RequestMapping("/api/users")
public class UserController {
private final UserRepository userRepository;
public UserController(UserRepository userRepository) {
this.userRepository = userRepository;
}
@GetMapping
public List<User> searchUsers(
@RequestParam(required = false) String status,
@RequestParam(required = false) String email,
@RequestParam(defaultValue = "0") int page,
@RequestParam(defaultValue = "20") int size) {
// GET /api/users?status=ACTIVE&page=2&size=10
// status and email are optional (null if not provided)
// page defaults to 0, size defaults to 20
return userRepository.findUsers(status, email, page, size);
}
@GetMapping("/search")
public List<User> searchByName(
@RequestParam String name, // required: returns 400 if missing
@RequestParam(defaultValue = "ASC") String sortOrder) {
return userRepository.findByNameContaining(name, sortOrder);
}
}
Common Mistakes & Pitfalls
- Making all params required: Freshers write
@RequestParam String statuswithoutrequired=false, so clients must always provide every parameter or get a 400 error. This breaks flexibility — optional filters should be optional. Userequired=falseordefaultValueto avoid this. - Forgetting type conversion errors: If a client sends
page=abcand your parameter isint, Spring throwsMethodArgumentTypeMismatchException. Freshers don't handle this with an exception handler, returning a confusing 500 instead of a clear 400. - Not using defaultValue for pagination: Writing
@RequestParam int pagewithout a default means clients must always specify page numbers. UsedefaultValue="0"soGET /usersworks without query params and still returns the first page.
Check your understanding
Answer these without looking back at the article.
- What happens if a required @RequestParam is missing?
- How do you bind a list of values from the query string with @RequestParam?
- What is the difference between required=false and providing a defaultValue?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.