Bird
0
0

Consider this controller method:

hard📝 Application Q9 of 15
Spring Boot - Request and Response Handling
Consider this controller method:
@GetMapping("/search/{type}")
public String search(@PathVariable String type, @RequestParam(required = false) String keyword) {
    if (keyword == null) return "Search all " + type;
    else return "Search " + type + " for " + keyword;
}

What is the output of accessing /search/book without query parameters?
ASearch all book
BSearch book for null
CError: Missing required query parameter 'keyword'
DSearch book for ''
Step-by-Step Solution
Solution:
  1. Step 1: Check if query parameter is optional

    @RequestParam(required = false) means 'keyword' can be null.
  2. Step 2: Analyze method logic for null keyword

    If keyword is null, method returns "Search all " + type.
  3. Final Answer:

    Search all book -> Option A
  4. Quick Check:

    Optional query param null handled correctly = output correct [OK]
Quick Trick: Use required=false to allow missing query params [OK]
Common Mistakes:
  • Expecting error when optional query param missing
  • Assuming empty string instead of null
  • Confusing path variable with query param

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Spring Boot Quizzes