In APIs, GET, PUT, POST, DELETE, and PATCH are HTTP methods used to interact with
resources. GET retrieves data, POST creates new resources, PUT updates entire
resources, DELETE removes resources, and PATCH updates specific parts of a
resource.
HTTP Methods Explained: Understanding GET, POST, PUT, and ...
Here's a breakdown:
GET:
Retrieves data from a specified resource. Think of it like asking a server for
information.
POST:
Sends data to the server to create a new resource. For example, submitting a form
to create a new user account.
PUT:
Replaces an existing resource with the data provided in the request. If the
resource doesn't exist, it may be created.
DELETE:
Removes a resource from the server.
PATCH:
Modifies only the specific fields of a resource, leaving other fields untouched.
Example:
Let's say we have a resource representing a user with fields like id, name, and
email.
GET /users/123 would retrieve the user with ID 123.
POST /users with a request body containing {"name": "John Doe", "email":
"[email protected]"} would create a new user.
PUT /users/123 with a request body containing {"name": "John Smith", "email":
"[email protected]"} would replace the entire user with ID 123 with the new
data.
DELETE /users/123 would remove the user with ID 123.
PATCH /users/123 with a request body containing {"email": "[email protected]"}
would only update the email of the user with ID 123.
@GetMapping: It maps the HTTP GET requests on the specific handler method. It is
used to create a web service endpoint that fetches It is used instead of using:
@RequestMapping(method = RequestMethod.GET)
@PostMapping: It maps the HTTP POST requests on the specific handler method. It is
used to create a web service endpoint that creates It is used instead of using:
@RequestMapping(method = RequestMethod.POST)
@PutMapping: It maps the HTTP PUT requests on the specific handler method. It is
used to create a web service endpoint that creates or updates It is used instead of
using: @RequestMapping(method = RequestMethod.PUT)
@DeleteMapping: It maps the HTTP DELETE requests on the specific handler method. It
is used to create a web service endpoint that deletes a resource. It is used
instead of using: @RequestMapping(method = RequestMethod.DELETE)
@PatchMapping: It maps the HTTP PATCH requests on the specific handler method. It
is used instead of using: @RequestMapping(method = RequestMethod.PATCH)