Challenge - 5 Problems
RequestParam Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What is the output when accessing the endpoint with query ?name=John
Consider this Spring Boot controller method:
What will be the response body when you visit
@GetMapping("/greet")
public String greet(@RequestParam String name) {
return "Hello, " + name + "!";
}What will be the response body when you visit
/greet?name=John?Spring Boot
@GetMapping("/greet") public String greet(@RequestParam String name) { return "Hello, " + name + "!"; }
Attempts:
2 left
💡 Hint
The @RequestParam annotation binds the query string parameter to the method argument.
✗ Incorrect
The query string parameter 'name' is passed as 'John', so the method returns 'Hello, John!'.
❓ state_output
intermediate2:00remaining
What happens if the query parameter is missing?
Given this method:
What happens when you call
@GetMapping("/welcome")
public String welcome(@RequestParam String user) {
return "Welcome, " + user;
}What happens when you call
/welcome without any query parameters?Spring Boot
@GetMapping("/welcome") public String welcome(@RequestParam String user) { return "Welcome, " + user; }
Attempts:
2 left
💡 Hint
By default, @RequestParam is required unless specified otherwise.
✗ Incorrect
Since 'user' parameter is required by default and missing, Spring returns 400 Bad Request.
📝 Syntax
advanced2:00remaining
Which option correctly sets a default value for a missing query parameter?
You want to make the query parameter optional and provide a default value "Guest" if missing. Which method signature is correct?
Attempts:
2 left
💡 Hint
The correct attribute name for default value is 'defaultValue'.
✗ Incorrect
Option B uses the correct attribute 'defaultValue' and does not set 'required' which defaults to true but is overridden by defaultValue.
🔧 Debug
advanced2:00remaining
Why does this code cause a 400 error when calling /search without parameters?
Look at this method:
Why does calling
@GetMapping("/search")
public String search(@RequestParam String query, @RequestParam(required = false) Integer page) {
return "Searching for " + query + " on page " + (page == null ? 1 : page);
}Why does calling
/search without query parameters cause a 400 error?Spring Boot
@GetMapping("/search") public String search(@RequestParam String query, @RequestParam(required = false) Integer page) { return "Searching for " + query + " on page " + (page == null ? 1 : page); }
Attempts:
2 left
💡 Hint
Check which parameters are required by default.
✗ Incorrect
'query' is required by default and missing causes 400 Bad Request. 'page' is optional.
🧠 Conceptual
expert3:00remaining
How does Spring handle multiple values for the same query parameter?
Given this method:
What is the output when calling
@GetMapping("/filter")
public String filter(@RequestParam List tags) {
return "Tags: " + String.join(",", tags);
} What is the output when calling
/filter?tags=java&tags=spring&tags=boot?Spring Boot
@GetMapping("/filter") public String filter(@RequestParam List<String> tags) { return "Tags: " + String.join(",", tags); }
Attempts:
2 left
💡 Hint
Spring can bind multiple query parameters with the same name into a List.
✗ Incorrect
Spring collects all 'tags' parameters into the List and joins them with commas.