Clean • Professional
HTTP methods define the action a client wants to perform on a resource. In Spring, we map them to controller methods using annotations like @GetMapping, @PostMapping, etc., or with @RequestMapping(method=...).

@GetMapping or @RequestMapping(method = RequestMethod.GET)Example:
@GetMapping("/users/{id}")
public UsergetUser(@PathVariable Long id) {
return userService.findById(id);
}
URL: /users/10 → Returns user with ID 10
@PostMappingExample:
@PostMapping("/users")
public UsercreateUser(@RequestBody User user) {
return userService.save(user);
}
URL: /users → Creates a new user
@PutMappingExample:
@PutMapping("/users/{id}")
public UserupdateUser(@PathVariable Long id,@RequestBody User user) {
return userService.update(id, user);
}
URL: /users/10 → Updates user with ID 10
@PatchMappingExample:
@PatchMapping("/users/{id}/email")
public UserupdateEmail(@PathVariable Long id,@RequestParam String email) {
return userService.updateEmail(id, email);
}
URL: /users/10/[email protected] → Updates only email
@DeleteMappingExample:
@DeleteMapping("/users/{id}")
publicvoiddeleteUser(@PathVariable Long id) {
userService.delete(id);
}
URL: /users/10 → Deletes user with ID 10
@RequestMapping(method = RequestMethod.OPTIONS)Example:
@RequestMapping(value = "/users", method = RequestMethod.OPTIONS)
public ResponseEntity<?> options() {
return ResponseEntity.ok().allow(HttpMethod.GET, HttpMethod.POST).build();
}
@RequestMapping(method = RequestMethod.HEAD)Example:
@RequestMapping(value = "/users/{id}", method = RequestMethod.HEAD)
public ResponseEntity<Void>head(@PathVariable Long id) {
return ResponseEntity.ok().build();
}
| HTTP Method | Purpose | Spring Annotation | Example URL | Idempotent? | Safe? |
|---|---|---|---|---|---|
| GET | Retrieve resource | @GetMapping | /users/10 | Yes | Yes |
| POST | Create resource | @PostMapping | /users | No | No |
| PUT | Update/Replace | @PutMapping | /users/10 | Yes | No |
| PATCH | Partial update | @PatchMapping | /users/10/email | No | No |
| DELETE | Remove resource | @DeleteMapping | /users/10 | Yes | No |
| OPTIONS | Resource options | @RequestMapping(method=OPTIONS) | /users | Yes | Yes |
| HEAD | Resource headers | @RequestMapping(method=HEAD) | /users/10 | Yes | Yes |
HTTP methods define how clients interact with server resources. In Spring MVC and Spring Boot, they are mapped to controller methods using annotations like @GetMapping, @PostMapping, @PutMapping, @PatchMapping, @DeleteMapping, or the generic @RequestMapping(method=...).