HTTP Methods in REST APIs
HTTP methods define the action performed on a resource. Using the correct method is fundamental to RESTful API design and is tested in Java developer interviews worldwide.
Method Reference
GET
Retrieve resource(s). Safe and idempotent. Never modify data.
POST
Create new resource. Not idempotent — repeated calls may create duplicates.
PUT
Replace entire resource. Idempotent.
PATCH
Partial update. Idempotent when designed correctly.
DELETE
Remove resource. Idempotent.
Spring Boot Example
@RestController
@RequestMapping("/api/products")
public class ProductController {
@GetMapping("/{id}")
public Product get(@PathVariable Long id) { ... }
@PostMapping
public ResponseEntity<Product> create(@RequestBody Product p) { ... }
@PutMapping("/{id}")
public Product update(@PathVariable Long id, @RequestBody Product p) { ... }
@DeleteMapping("/{id}")
public ResponseEntity<Void> delete(@PathVariable Long id) { ... }
}Frequently Asked Questions
POST vs PUT for updates?▼
Use PUT to replace the entire resource. Use PATCH for partial updates. POST is for creating new resources.
