@PutMapping and @DeleteMapping help your web app handle updates and deletions of data easily. They connect web requests to your code actions.
@PutMapping and @DeleteMapping in Spring Boot
@PutMapping("/path") public ResponseEntity<Type> updateMethod(@RequestBody Type data) { // update logic } @DeleteMapping("/path/{id}") public ResponseEntity<Void> deleteMethod(@PathVariable Long id) { // delete logic }
@PutMapping is for updating data, usually with a full new version of the resource.
@DeleteMapping is for removing data identified by a path variable like an ID.
@PutMapping("/users/{id}") public ResponseEntity<User> updateUser(@PathVariable Long id, @RequestBody User user) { // update user with id }
@DeleteMapping("/users/{id}")
public ResponseEntity<Void> deleteUser(@PathVariable Long id) {
// delete user by id
}
This simple controller stores users in memory. You can update a user's name or delete a user by their ID using @PutMapping and @DeleteMapping.
import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PutMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RestController; import java.util.HashMap; import java.util.Map; @RestController public class UserController { private Map<Long, String> users = new HashMap<>(); public UserController() { users.put(1L, "Alice"); users.put(2L, "Bob"); } @PutMapping("/users/{id}") public ResponseEntity<String> updateUser(@PathVariable Long id, @RequestBody String newName) { if (!users.containsKey(id)) { return ResponseEntity.notFound().build(); } users.put(id, newName); return ResponseEntity.ok("User updated to: " + newName); } @DeleteMapping("/users/{id}") public ResponseEntity<String> deleteUser(@PathVariable Long id) { if (!users.containsKey(id)) { return ResponseEntity.notFound().build(); } users.remove(id); return ResponseEntity.ok("User deleted with id: " + id); } }
Use @PutMapping for full updates; partial updates usually use @PatchMapping.
Always check if the resource exists before updating or deleting to avoid errors.
Return proper HTTP status codes like 404 if the resource is not found.
@PutMapping handles updating existing data via HTTP PUT requests.
@DeleteMapping handles deleting data via HTTP DELETE requests.
Both help build clear and standard REST APIs in Spring Boot.
Practice
@PutMapping annotation in Spring Boot?Solution
Step 1: Understand HTTP methods
HTTP PUT is used to update existing data on the server.Step 2: Match annotation to HTTP method
@PutMappingmaps HTTP PUT requests to controller methods.Final Answer:
To handle HTTP PUT requests for updating existing resources -> Option BQuick Check:
@PutMapping = update data [OK]
- Confusing @PutMapping with @PostMapping
- Thinking @PutMapping creates new data
- Mixing HTTP methods like GET or DELETE
@DeleteMapping?Solution
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.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.Final Answer:
@DeleteMapping("/items/{id}") public void deleteItem(@PathVariable int id) {} -> Option CQuick Check:
Delete by ID needs @PathVariable and matching URL [OK]
- Omitting @PathVariable annotation
- Using @RequestParam instead of @PathVariable for path params
- Not matching method parameter to URL placeholder
/users/5 with a valid JSON body?@PutMapping("/users/{id}")
public User updateUser(@PathVariable int id, @RequestBody User user) {
user.setId(id);
return userService.save(user);
}Solution
Step 1: Analyze method parameters and annotations
The method uses @PutMapping with path variable id and @RequestBody user object. It sets user ID from path.Step 2: Understand method behavior on PUT request
It updates the user ID to match URL, then calls save to update and returns updated user.Final Answer:
The user with ID 5 will be updated with the JSON data and returned -> Option AQuick Check:
PUT updates resource with URL ID and JSON body [OK]
- Assuming PUT creates new resource instead of updating
- Ignoring @PathVariable ID and using JSON ID only
- Missing @RequestBody causing request failure
@DeleteMapping("/users")
public void deleteUser(@PathVariable int id) {
userService.deleteById(id);
}Solution
Step 1: Check URL and method parameters
The method expects @PathVariable int id but URL is "/users" without {id} placeholder.Step 2: Understand path variable binding
Without {id} in URL, Spring cannot bind path variable, causing error.Final Answer:
The URL does not include the path variable placeholder {id} -> Option AQuick Check:
Path variable needs matching {id} in URL [OK]
- Forgetting to add {id} in URL path
- Using @PathVariable without URL placeholder
- Confusing @RequestParam with @PathVariable
@PutMapping?Solution
Step 1: Check for product existence before update
@PutMapping("/products/{id}") public ResponseEntity<Product> updatePrice(@PathVariable int id, @RequestBody Product p) { return productService.findById(id) .map(existing -> { existing.setPrice(p.getPrice()); productService.save(existing); return ResponseEntity.ok(existing); }) .orElse(ResponseEntity.notFound().build()); } uses productService.findById(id) to check if product exists, returning 404 if not.Step 2: Update price only if product found and return proper HTTP response
The map lambda updates the price on the existing product, saves it, and returns ResponseEntity.ok(existing); orElse returns notFound().Final Answer:
@PutMapping("/products/{id}") public ResponseEntity<Product> updatePrice(@PathVariable int id, @RequestBody Product p) { return productService.findById(id) .map(existing -> { existing.setPrice(p.getPrice()); productService.save(existing); return ResponseEntity.ok(existing); }) .orElse(ResponseEntity.notFound().build()); } -> Option DQuick Check:
Check existence, update, return 404 if missing [OK]
- Not checking if product exists before update
- Using @RequestParam instead of @PathVariable for ID
- Not returning 404 when product missing
Ready to prove it?
Pass the test and this pattern moves unknown
