0
0
Spring Bootframework~20 mins

@RequestParam for query strings in Spring Boot - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
RequestParam Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output when accessing the endpoint with query ?name=John
Consider this Spring Boot controller method:
@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 + "!";
}
A"Hello, John!"
B"Hello, null!"
CHTTP 400 Bad Request error
D"Hello, !"
Attempts:
2 left
💡 Hint
The @RequestParam annotation binds the query string parameter to the method argument.
state_output
intermediate
2:00remaining
What happens if the query parameter is missing?
Given this method:
@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;
}
AHTTP 500 Internal Server Error
B"Welcome, null"
CHTTP 400 Bad Request error
D"Welcome, "
Attempts:
2 left
💡 Hint
By default, @RequestParam is required unless specified otherwise.
📝 Syntax
advanced
2: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?
A
@GetMapping("/hello")
public String hello(@RequestParam(required = false, default = "Guest") String name) {
    return "Hi, " + name;
}
B
@GetMapping("/hello")
public String hello(@RequestParam(defaultValue = "Guest") String name) {
    return "Hi, " + name;
}
C
@GetMapping("/hello")
public String hello(@RequestParam(required = true, defaultValue = "Guest") String name) {
    return "Hi, " + name;
}
D
@GetMapping("/hello")
public String hello(@RequestParam(optional = true, defaultValue = "Guest") String name) {
    return "Hi, " + name;
}
Attempts:
2 left
💡 Hint
The correct attribute name for default value is 'defaultValue'.
🔧 Debug
advanced
2:00remaining
Why does this code cause a 400 error when calling /search without parameters?
Look at this method:
@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);
}
ABecause 'query' is required and missing in the request
BBecause the method returns a String instead of ResponseEntity
CBecause both parameters are required by default
DBecause 'page' is an Integer and cannot be null
Attempts:
2 left
💡 Hint
Check which parameters are required by default.
🧠 Conceptual
expert
3:00remaining
How does Spring handle multiple values for the same query parameter?
Given this method:
@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);
}
A"Tags: java"
B"Tags: [java, spring, boot]"
CHTTP 400 Bad Request error
D"Tags: java,spring,boot"
Attempts:
2 left
💡 Hint
Spring can bind multiple query parameters with the same name into a List.