Bird
0
0

Examine the following Spring Boot controller method intended to delete an order by ID:

medium📝 Debug Q7 of 15
Spring Boot - REST Controllers
Examine the following Spring Boot controller method intended to delete an order by ID:
@DeleteMapping("/orders")
public void removeOrder(@PathVariable Long orderId) {
    // deletion logic
}

What is the issue with this method?
AThe URL mapping does not include the path variable placeholder for orderId.
BThe method should use @RequestParam instead of @PathVariable for orderId.
CThe method should return ResponseEntity instead of void.
DThe @DeleteMapping annotation should be replaced with @PostMapping.
Step-by-Step Solution
Solution:
  1. Step 1: Check URL mapping

    The URL mapping is "/orders" but the method expects a path variable orderId.
  2. Step 2: Path variable placeholder missing

    The URL should include "{orderId}" to bind the path variable correctly.
  3. Step 3: Correct mapping

    It should be @DeleteMapping("/orders/{orderId}") to match the @PathVariable parameter.
  4. Final Answer:

    Option A -> Option A
  5. Quick Check:

    Path variable must be declared in URL mapping [OK]
Quick Trick: Path variables must appear in URL mapping [OK]
Common Mistakes:
  • Forgetting to include path variable in the URL
  • Confusing @PathVariable with @RequestParam
  • Ignoring return types for REST methods

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Spring Boot Quizzes