Query parameters are key=value pairs added to the end of a URL after ? and joined with &, like /students/search?name=ravi&department=CSE. In a controller, @RequestParam reads each one and passes it into the method, converting it to the parameter’s type. They are used for optional search, filtering, sorting, and paging, where the URL still points to the same resource list. Path variables identify one specific resource, while query parameters refine or filter a collection.
Features
- Required:
@RequestParam String email. If it is missing, Spring returns 400. - Optional:
@RequestParam(required = false) String name, which is null when not supplied. - Default value:
@RequestParam(defaultValue = "10") int limit. - Optional wrapper:
@RequestParam Optional<String> name. - Multiple values:
@RequestParam List<String> departmentsfor?departments=CSE&departments=ECE. - All parameters as a Map:
@RequestParam Map<String, String> params.
