@RequestParam helps you get values from the URL query string easily. It lets your program read small pieces of information sent by the user in the web address.
@RequestParam for query strings in Spring Boot
@RequestParam(type) parameterNameYou usually put @RequestParam before a method parameter in a controller.
You can set a default value or make the parameter optional.
@RequestParam String name
@RequestParam(required = false) Integer age
@RequestParam(defaultValue = "guest") String userThis controller has a GET endpoint /greet that reads 'name' and optional 'lang' from the query string. It returns a greeting in English or Spanish based on 'lang'.
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; @RestController public class GreetingController { @GetMapping("/greet") public String greetUser(@RequestParam String name, @RequestParam(required = false, defaultValue = "en") String lang) { if (lang.equals("es")) { return "Hola, " + name + "!"; } else { return "Hello, " + name + "!"; } } }
If you forget to send a required parameter, Spring will return an error automatically.
You can use @RequestParam with many types like int, boolean, or even lists.
Always validate or sanitize query parameters to keep your app safe.
@RequestParam reads values from the URL query string.
You can make parameters required, optional, or give default values.
It helps your app respond differently based on user input in the URL.
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
