Bird
0
0

Which of the following is the correct syntax to define a method that deletes a resource by ID using @DeleteMapping?

easy📝 Syntax Q12 of 15
Spring Boot - REST Controllers
Which of the following is the correct syntax to define a method that deletes a resource by ID using @DeleteMapping?
A@DeleteMapping("/items/{id}") public void deleteItem(int id) {}
B@DeleteMapping("/items") public void deleteItem(int id) {}
C@DeleteMapping("/items/{id}") public void deleteItem(@PathVariable int id) {}
D@DeleteMapping("/items") public void deleteItem(@RequestParam int id) {}
Step-by-Step Solution
Solution:
  1. Step 1: Identify correct URL pattern and annotation

    To delete by ID, URL should include path variable like /items/{id} and method parameter annotated with @PathVariable.
  2. Step 2: Check method signature matches path variable

    @DeleteMapping("/items/{id}") public void deleteItem(@PathVariable int id) {} correctly uses @DeleteMapping with path and @PathVariable int id parameter.
  3. Final Answer:

    @DeleteMapping("/items/{id}") public void deleteItem(@PathVariable int id) {} -> Option C
  4. Quick Check:

    Delete by ID needs @PathVariable and matching URL [OK]
Quick Trick: Use @PathVariable with URL {id} in @DeleteMapping [OK]
Common Mistakes:
  • Omitting @PathVariable annotation
  • Using @RequestParam instead of @PathVariable for path params
  • Not matching method parameter to URL placeholder

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Spring Boot Quizzes