Complete the code to bind a query string parameter named 'name' to the method parameter.
public String greet(@[1]("name") String userName) { return "Hello, " + userName + "!"; }
The @RequestParam annotation binds a query string parameter to a method parameter in Spring Boot.
Complete the code to make the 'age' query parameter optional with a default value of 18.
public String checkAge(@RequestParam(value = "age", required = [1], defaultValue = "18") int age) { return "Age is " + age; }
Setting required = false makes the query parameter optional. If missing, the default value is used.
Fix the error in the method parameter to correctly bind the 'id' query parameter as a Long.
public String getUser(@RequestParam("id") [1] userId) { return "User ID: " + userId; }
The query parameter 'id' should be bound to a Long type to match expected data.
Fill both blanks to bind two query parameters 'city' and 'country' with default values.
public String location(@RequestParam(value = "city", defaultValue = [1]) String city, @RequestParam(value = "country", defaultValue = [2]) String country) { return city + ", " + country; }
Default values are strings and must be quoted. Here, city defaults to "New York" and country to "USA".
Fill all three blanks to create a method that binds 'page', 'size', and 'sort' query parameters with defaults.
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; }
Default values for page and size are numbers (1 and 10), and sort is a string ("id"). Numbers do not need quotes.