0
0
Spring Bootframework~10 mins

@RequestParam for query strings in Spring Boot - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to bind a query string parameter named 'name' to the method parameter.

Spring Boot
public String greet(@[1]("name") String userName) {
    return "Hello, " + userName + "!";
}
Drag options to blanks, or click blank then click option'
ARequestParam
BPathVariable
CAutowired
DRequestBody
Attempts:
3 left
💡 Hint
Common Mistakes
Using @PathVariable instead of @RequestParam for query parameters.
Forgetting to specify the parameter name inside the annotation.
2fill in blank
medium

Complete the code to make the 'age' query parameter optional with a default value of 18.

Spring Boot
public String checkAge(@RequestParam(value = "age", required = [1], defaultValue = "18") int age) {
    return "Age is " + age;
}
Drag options to blanks, or click blank then click option'
Atrue
Bfalse
Cnull
Doptional
Attempts:
3 left
💡 Hint
Common Mistakes
Setting required to true when the parameter should be optional.
Not providing a defaultValue when required is false.
3fill in blank
hard

Fix the error in the method parameter to correctly bind the 'id' query parameter as a Long.

Spring Boot
public String getUser(@RequestParam("id") [1] userId) {
    return "User ID: " + userId;
}
Drag options to blanks, or click blank then click option'
Aint
Bboolean
CString
DLong
Attempts:
3 left
💡 Hint
Common Mistakes
Using primitive int instead of Long for large IDs.
Using String when a numeric type is expected.
4fill in blank
hard

Fill both blanks to bind two query parameters 'city' and 'country' with default values.

Spring Boot
public String location(@RequestParam(value = "city", defaultValue = [1]) String city, @RequestParam(value = "country", defaultValue = [2]) String country) {
    return city + ", " + country;
}
Drag options to blanks, or click blank then click option'
A"Unknown"
B"USA"
C"New York"
D"None"
Attempts:
3 left
💡 Hint
Common Mistakes
Not using quotes around default string values.
Mixing up city and country default values.
5fill in blank
hard

Fill all three blanks to create a method that binds 'page', 'size', and 'sort' query parameters with defaults.

Spring Boot
public String listItems(@RequestParam(value = "page", defaultValue = [1]) int page, @RequestParam(value = "size", defaultValue = [2]) int size, @RequestParam(value = "sort", defaultValue = [3]) String sort) {
    return "Page: " + page + ", Size: " + size + ", Sort by: " + sort;
}
Drag options to blanks, or click blank then click option'
A"name"
B"id"
C"10"
D"1"
Attempts:
3 left
💡 Hint
Common Mistakes
Quoting numeric default values.
Using wrong default values for parameters.