Bird
0
0

Which method signature correctly implements this behavior?

hard📝 Application Q15 of 15
Spring Boot - Request and Response Handling
You want to create a Spring Boot endpoint that handles a user ID as a path variable and optionally accepts a query parameter filter. If filter is missing, it should default to "all". Which method signature correctly implements this behavior?
Apublic String getUser(@PathVariable String userId, @RequestParam Optional<String> filter)
Bpublic String getUser(@RequestParam String userId, @RequestParam String filter)
Cpublic String getUser(@PathVariable String userId, @RequestParam(required = true) String filter)
Dpublic String getUser(@PathVariable String userId, @RequestParam(defaultValue = "all") String filter)
Step-by-Step Solution
Solution:
  1. Step 1: Identify path variable usage

    User ID is part of the URL path, so use @PathVariable for userId.
  2. Step 2: Handle optional query parameter with default

    Use @RequestParam with defaultValue = "all" to provide a default when filter is missing.
  3. Final Answer:

    public String getUser(@PathVariable String userId, @RequestParam(defaultValue = "all") String filter) -> Option D
  4. Quick Check:

    Default query param value = @RequestParam(defaultValue) [OK]
Quick Trick: Use @RequestParam(defaultValue) for optional query params [OK]
Common Mistakes:
  • Using @RequestParam(required = true) for optional params
  • Using @RequestParam without defaultValue causing errors
  • Confusing @RequestParam and @PathVariable roles

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Spring Boot Quizzes