Bird
0
0

Identify the error in this method:

medium📝 Debug Q6 of 15
Spring Boot - REST Controllers
Identify the error in this method:
@GetMapping("/user")
public String getUser(@RequestParam int id, @RequestParam String name) {
    return name + " with ID " + id;
}

When called with URL /user?id=5, what happens?
AReturns 'null with ID 5'
BRuntime error due to missing 'name' parameter
CReturns ' with ID 5'
DReturns 'name with ID 5'
Step-by-Step Solution
Solution:
  1. Step 1: Check required parameters

    Both 'id' and 'name' are required by default. URL provides only 'id'.
  2. Step 2: Understand missing parameter effect

    Missing 'name' causes Spring to throw a runtime error for missing required parameter.
  3. Final Answer:

    Runtime error due to missing 'name' parameter -> Option B
  4. Quick Check:

    Missing required param causes error [OK]
Quick Trick: All required params must be present or error occurs [OK]
Common Mistakes:
  • Assuming missing String param defaults to null
  • Expecting method to run with missing params
  • Confusing optional and required parameters

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Spring Boot Quizzes