0
0
Spring Bootframework~10 mins

@PutMapping and @DeleteMapping in Spring Boot - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - @PutMapping and @DeleteMapping
Client sends HTTP PUT request
@PutMapping method matches URL
Method updates resource
Return updated resource or status
Client sends HTTP DELETE request
@DeleteMapping method matches URL
Method deletes resource
Return deletion confirmation or status
The flow shows how HTTP PUT and DELETE requests are handled by Spring Boot methods annotated with @PutMapping and @DeleteMapping to update or delete resources.
Execution Sample
Spring Boot
@PutMapping("/items/{id}")
public Item updateItem(@PathVariable int id, @RequestBody Item newItem) {
    // update logic
    return updatedItem;
}

@DeleteMapping("/items/{id}")
public void deleteItem(@PathVariable int id) {
    // delete logic
}
This code updates an item with PUT and deletes an item with DELETE based on the item id.
Execution Table
StepRequest TypeURLMethod MatchedAction TakenResponse
1PUT/items/5@PutMapping("/items/{id}")Update item with id=5Return updated item
2DELETE/items/3@DeleteMapping("/items/{id}")Delete item with id=3Return deletion confirmation
3PUT/items/10@PutMapping("/items/{id}")Update item with id=10Return updated item
4DELETE/items/99@DeleteMapping("/items/{id}")Delete item with id=99Return deletion confirmation
5GET/items/5No matchNo action404 Not Found
💡 Execution stops when no matching mapping is found or after response is sent.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4Final
idN/A531099N/A
newItemN/AProvided in PUT bodyN/AProvided in PUT bodyN/AN/A
itemsCollectionInitial itemsItem 5 updatedItem 3 deletedItem 10 updatedItem 99 deletedFinal state
Key Moments - 3 Insights
Why does a GET request to /items/5 not match @PutMapping or @DeleteMapping?
Because @PutMapping and @DeleteMapping only handle PUT and DELETE HTTP methods respectively, not GET. See execution_table row 5 where GET has no match.
What happens if the item id does not exist when updating or deleting?
The method can return a 404 Not Found or handle the error internally. This is not shown in the table but is common practice.
How does Spring know which method to call for a request?
Spring matches the HTTP method (PUT or DELETE) and the URL pattern to the annotated method, as shown in execution_table steps 1-4.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the response when a DELETE request is sent to /items/3?
AReturn updated item
BReturn deletion confirmation
C404 Not Found
DNo action
💡 Hint
Check execution_table row 2 under Response column.
At which step does the PUT request update the item with id=10?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Look at execution_table row 3 for PUT requests and id values.
If a GET request is sent to /items/5, what will happen according to the execution_table?
ANo match, 404 Not Found
BDelete item with id=5
CUpdate item with id=5
DReturn deletion confirmation
💡 Hint
See execution_table row 5 for GET request handling.
Concept Snapshot
@PutMapping and @DeleteMapping handle HTTP PUT and DELETE requests.
Use @PutMapping("/path/{id}") to update a resource by id.
Use @DeleteMapping("/path/{id}") to delete a resource by id.
Spring matches request method and URL to call the correct handler.
Return updated resource or confirmation status after action.
Full Transcript
This visual execution shows how Spring Boot handles HTTP PUT and DELETE requests using @PutMapping and @DeleteMapping annotations. When a client sends a PUT request to a URL like /items/5, Spring calls the method annotated with @PutMapping matching that URL pattern. The method updates the item with id 5 and returns the updated item. Similarly, a DELETE request to /items/3 calls the @DeleteMapping method to delete the item with id 3 and returns a confirmation. If a request uses a different HTTP method like GET, no matching method is found, resulting in a 404 Not Found. Variables like the item id and the collection of items change as updates and deletions happen. This step-by-step trace helps beginners see how Spring routes requests and changes state.