HTTP Method Annotations in Spring — GET, POST, PUT, DELETE, PATCH
Fast recall
Quick revision
Core answer
Knowledge of RESTful conventions and proper HTTP method semantics, which is fundamental to designing clean REST APIs.
Using @PostMapping for all operations including updates, when @PutMapping or @PatchMapping is semantically correct for modifications.
Explain the idempotency property: GET, PUT, and DELETE are idempotent (multiple identical requests produce the same result), while POST is not, which affects API design decisions.
Interview-ready guide
Interview answer
Spring will happily map a @GetMapping method that deletes data, but that violates HTTP safety and can be triggered by crawlers or caches. @PostMapping is not limited to creation. @PutMapping can create a resource at a client-known URI or replace it. @DeleteMapping can return different success statuses while remaining idempotent: repeating it should not create additional intended state changes.
HTTP Method Annotations in Spring — GET, POST, PUT, DELETE, PATCH
Direct Answer
@GetMapping, @PostMapping, @PutMapping, @DeleteMapping, and @PatchMapping are composed @RequestMapping annotations that restrict handler selection to the corresponding HTTP method. They do not enforce REST behavior. GET is defined as safe; PUT and DELETE are idempotent by HTTP semantics; POST is general processing; PATCH applies a partial modification document whose media type defines its rules.
Code Example
@RestController
@RequestMapping("/api/products")
public class ProductController {
private final ProductService productService;
public ProductController(ProductService productService) {
this.productService = productService;
}
@GetMapping("/{id}")
public Product getProduct(@PathVariable Long id) { // Idempotent: safe to retry
return productService.findById(id);
}
@PostMapping
public Product createProduct(@RequestBody Product product) { // NOT idempotent: retrying creates duplicates
return productService.create(product);
}
@PutMapping("/{id}")
public Product updateProduct(@PathVariable Long id, @RequestBody Product product) { // Idempotent: same result on repeat
return productService.replace(id, product);
}
@PatchMapping("/{id}")
public Product patchProduct(@PathVariable Long id, @RequestBody Map<String, Object> updates) { // Partial update
return productService.partialUpdate(id, updates);
}
@DeleteMapping("/{id}")
public void deleteProduct(@PathVariable Long id) { // Idempotent: deleting twice has same effect
productService.delete(id);
}
}
Common Mistakes & Pitfalls
- Using POST for everything: Freshers often use POST for all operations including updates because it works, but this violates REST conventions and breaks caching, idempotency expectations. Clients can't safely retry POST requests.
- Confusing PUT and PATCH: Using PUT when only updating one field sends unnecessary data and risks overwriting fields not included in the request. PUT expects the full object; PATCH expects only changes.
- GET with request body: Trying to send data in a GET request body. GET should only use query parameters and path variables; the body is typically ignored by servers.
- Ignoring idempotency: Not understanding that multiple identical PUT or DELETE requests should produce the same result, while POST creates duplicates. This affects retry logic and error handling in production.
Check your understanding
Answer these without looking back at the article.
- What is idempotency and which HTTP methods are idempotent?
- What is the difference between PUT and PATCH?
- What HTTP status code should a POST request return on success?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.