Discover how a simple annotation can save you from messy URL parsing headaches!
Why @RequestParam for query strings in Spring Boot? - Purpose & Use Cases
Imagine building a web app where users send data through URLs, like searching for products with filters. You have to manually read and parse each query string from the URL to get user input.
Manually extracting query strings means writing repetitive code to parse strings, handle missing or wrong data, and convert types. This is slow, error-prone, and makes your code messy and hard to maintain.
@RequestParam lets Spring Boot automatically grab query string values and convert them to the right type. It cleans your code, handles defaults, and makes your controller methods simple and clear.
String name = request.getParameter("name"); int age = Integer.parseInt(request.getParameter("age"));
@GetMapping("/user")
public String getUser(@RequestParam String name, @RequestParam int age) { ... }This makes building web APIs faster and safer by letting you focus on logic, not parsing URL details.
When a user searches a store with URL like /search?category=books&price=20, @RequestParam quickly gets those values so you can show matching products.
Manually parsing query strings is tedious and error-prone.
@RequestParam automates extracting and converting query parameters.
This leads to cleaner, easier-to-read controller code.
Practice
@RequestParam annotation do in a Spring Boot controller?Solution
Step 1: Understand the role of @RequestParam
@RequestParam is used to get values from the URL query string, like ?name=John.Step 2: Compare with other annotations
@RequestMapping maps URLs, @RequestParam extracts query parameters, others handle HTTP methods or errors.Final Answer:
It extracts query string parameters from the URL and passes them to the method. -> Option BQuick Check:
@RequestParam reads query parameters = A [OK]
- Confusing @RequestParam with @PathVariable
- Thinking it maps URLs instead of parameters
- Assuming it handles HTTP methods
@RequestParam?Solution
Step 1: Identify correct attributes for optional param
To make a parameter optional, use required = false and provide defaultValue as a string.Step 2: Check syntax correctness
@RequestParam(name = "id", required = false, defaultValue = "0") int id uses required = false and defaultValue = "0" correctly; others use invalid attributes or miss required parts.Final Answer:
@RequestParam(name = "id", required = false, defaultValue = "0") int id -> Option CQuick Check:
Optional param syntax = A [OK]
- Using 'optional' instead of 'required'
- Setting defaultValue as a number instead of string
- Omitting required attribute
@GetMapping("/greet")
public String greet(@RequestParam String name) {
return "Hello, " + name + "!";
}What will be the output when accessing
/greet?name=Anna?Solution
Step 1: Understand @RequestParam behavior
The method expects a query parameter 'name' which is required by default.Step 2: Check the URL query string
The URL provides ?name=Anna, so the method receives "Anna" as the parameter.Final Answer:
Hello, Anna! -> Option AQuick Check:
Query param 'name' = Anna, output greeting = D [OK]
- Assuming missing param causes null instead of error
- Confusing query string with path variable
- Expecting default value without setting one
@GetMapping("/search")
public String search(@RequestParam int page) {
return "Page: " + page;
}What happens if the URL
/search is accessed without any query parameters?Solution
Step 1: Check @RequestParam default behavior
By default, @RequestParam is required. Since 'page' is primitive int, it cannot be null.Step 2: Analyze missing parameter effect
Accessing /search without 'page' causes Spring to reject the request with 400 Bad Request due to missing required param.Final Answer:
The method throws a 400 Bad Request error -> Option DQuick Check:
Missing required param causes 400 error = C [OK]
- Assuming default 0 for missing int param
- Expecting null for primitive types
- Thinking method returns empty string on error
sort with default value "asc" and a required parameter page. Which method signature is correct?Solution
Step 1: Set optional param with default value
Use required = false and defaultValue = "asc" for the optional 'sort' parameter.Step 2: Ensure required param is declared correctly
The 'page' parameter is required by default, so just @RequestParam int page is enough.Step 3: Evaluate each option
@GetMapping("/items") public String items(@RequestParam(required = false, defaultValue = "asc") String sort, @RequestParam int page) { ... } correctly combines required=false and defaultValue for 'sort' and required 'page'. Incorrect options include one with required=true and defaultValue (still required on missing), one with required=false without defaultValue (null on missing), and one with invalid syntax.Final Answer:
@GetMapping("/items") public String items(@RequestParam(required = false, defaultValue = "asc") String sort, @RequestParam int page) { ... } -> Option AQuick Check:
Optional with default + required param = B [OK]
- Omitting required=false for optional param
- Trying to assign default value in method signature
- Forgetting defaultValue causes errors
Ready to prove it?
Pass the test and this pattern moves unknown
