@RequestParam extracts a value from the HTTP request's query string or form data and binds it directly to a controller method parameter, saving you from manually parsing HttpServletRequest.
Key Points: • By default the parameter is required; a missing value causes a 400 Bad Request unless required=false is set. • A defaultValue can be supplied so an optional parameter falls back to a sensible value instead of null. • The annotation can bind to any simple type Spring knows how to convert, such as String, int, or an enum. • When the method parameter name matches the request parameter name, the name attribute can be omitted (with -parameters compiler flag or explicit naming otherwise). • It works alongside @ModelAttribute and @PathVariable, each targeting a different part of the request.
Example: A search endpoint like /search?keyword=laptop&page=1 can capture those values with @RequestParam String keyword and @RequestParam(defaultValue = "0") int page, avoiding manual request.getParameter() calls.
Code Example:
@GetMapping("/search")
public String search(@RequestParam String keyword,
@RequestParam(defaultValue = "0") int page) {
...
}Interview Tip: A concise interview answer is:
"@RequestParam binds a query string or form parameter directly to a method argument, so I don't have to manually pull it off the HttpServletRequest. I can mark it optional with required=false, give it a defaultValue, and Spring converts it to the target type automatically."