Bird
0
0

Identify the error in this Spring Boot controller method intended to delete a user by ID:

medium📝 Debug Q14 of 15
Spring Boot - REST Controllers
Identify the error in this Spring Boot controller method intended to delete a user by ID:
@DeleteMapping("/users")
public void deleteUser(@PathVariable int id) {
    userService.deleteById(id);
}
AThe URL does not include the path variable placeholder {id}
BThe method should use @RequestParam instead of @PathVariable
CThe method should return a value instead of void
DThe @DeleteMapping annotation is missing
Step-by-Step Solution
Solution:
  1. Step 1: Check URL and method parameters

    The method expects @PathVariable int id but URL is "/users" without {id} placeholder.
  2. Step 2: Understand path variable binding

    Without {id} in URL, Spring cannot bind path variable, causing error.
  3. Final Answer:

    The URL does not include the path variable placeholder {id} -> Option A
  4. Quick Check:

    Path variable needs matching {id} in URL [OK]
Quick Trick: Match @PathVariable with {id} in URL path [OK]
Common Mistakes:
  • Forgetting to add {id} in URL path
  • Using @PathVariable without URL placeholder
  • Confusing @RequestParam with @PathVariable

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Spring Boot Quizzes