Bird
0
0

Given the controller method:

medium📝 component behavior Q13 of 15
Spring Boot - Request and Response Handling
Given the controller method:
@GetMapping("/items/{itemId}")
public String getItem(@PathVariable String itemId, @RequestParam(defaultValue = "asc") String order) {
    return itemId + "-" + order;
}

What will be the output when a client requests /items/42?order=desc?
A"42-asc"
B"42-desc"
C"desc-42"
DRuntime error due to missing parameter
Step-by-Step Solution
Solution:
  1. Step 1: Identify values from URL

    Path variable itemId is "42" from /items/42. Query parameter order is "desc" from ?order=desc.
  2. Step 2: Understand method return

    The method returns itemId + "-" + order, so "42-desc".
  3. Final Answer:

    "42-desc" -> Option B
  4. Quick Check:

    Path variable + query param = "42-desc" [OK]
Quick Trick: Path variable fills {itemId}, query param fills order value [OK]
Common Mistakes:
  • Ignoring query parameter and using default
  • Mixing order of concatenation
  • Expecting error when query param is present

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Spring Boot Quizzes