What is the difference between @GetMapping, @PostMapping, @PutMapping, and @DeleteMapping?
In Spring Boot, the `@GetMapping`, `@PostMapping`, `@PutMapping`, and `@DeleteMapping` annotations are specialized, composed annotations that map incoming HTTP requests to specific handler methods within your controller classes. They are convenience annotations built on top of the more general `@RequestMapping` annotation, making it simpler to define RESTful endpoints based on standard HTTP methods.
@GetMapping
The @GetMapping annotation is used to map HTTP GET requests to a specific handler method. It is a composed annotation that acts as a shortcut for @RequestMapping(method = RequestMethod.GET).
Purpose: Primarily used for retrieving data from the server. GET requests should only fetch data and have no other effect on the data. They are considered 'safe' and 'idempotent'.
Characteristics:
- HTTP Method: GET
- Purpose: Read/Retrieve resources.
- Idempotent: Yes (multiple identical requests have the same effect as a single one).
- Safe: Yes (does not change the state of the server).
@PostMapping
The @PostMapping annotation is used to map HTTP POST requests to a specific handler method. It is a composed annotation that acts as a shortcut for @RequestMapping(method = RequestMethod.POST).
Purpose: Primarily used for creating new resources on the server or submitting data that results in a state change or an action. POST requests are typically used when the server assigns the new resource's URI.
Characteristics:
- HTTP Method: POST
- Purpose: Create new resources or submit data for processing.
- Idempotent: No (multiple identical requests might create multiple resources or cause different side effects).
- Safe: No (changes the state of the server).
@PutMapping
The @PutMapping annotation is used to map HTTP PUT requests to a specific handler method. It is a composed annotation that acts as a shortcut for @RequestMapping(method = RequestMethod.PUT).
Purpose: Primarily used for updating an existing resource or creating a resource if it does not already exist at a *specified URI*. The client typically provides the complete new state of the resource.
Characteristics:
- HTTP Method: PUT
- Purpose: Update an existing resource or create one at a known URI.
- Idempotent: Yes (multiple identical requests will result in the same resource state).
- Safe: No (changes the state of the server).
@DeleteMapping
The @DeleteMapping annotation is used to map HTTP DELETE requests to a specific handler method. It is a composed annotation that acts as a shortcut for @RequestMapping(method = RequestMethod.DELETE).
Purpose: Primarily used for deleting a specified resource from the server.
Characteristics:
- HTTP Method: DELETE
- Purpose: Delete a specific resource.
- Idempotent: Yes (deleting a resource multiple times has the same effect as deleting it once – the resource is still gone).
- Safe: No (changes the state of the server).
Key Differences Summary
| Annotation | HTTP Method | Primary Purpose | Idempotent? | Safe? |
|---|---|---|---|---|
| @GetMapping | GET | Retrieve data | Yes | Yes |
| @PostMapping | POST | Create new resource, submit data | No | No |
| @PutMapping | PUT | Update existing resource or create at specific URI | Yes | No |
| @DeleteMapping | DELETE | Delete resource | Yes | No |