Mapping annotation in Spring Boot

Mapping annotation in Spring simply informs that this method handles HTTP requests as per their respective verb, like GET, POST, PATCH, PUT, etc. Although the annotation maps the request to the method, the logic for how the resource is replaced/created/deleted is up to the developer.

  • @PatchMapping Informs Spring that this method handles HTTP PATCH requests. It doesn’t define how partial updates should be applied — that logic is up to you.
  • @PutMapping Informs Spring that this method handles HTTP PUT requests. It is used to completely replace an existing resource. If the resource does not exist, depending on the implementation, it may be created. The request body should contain the entire updated resource; omitting fields may result in data loss unless handled carefully.
  • @GetMapping Informs Spring that this method handles HTTP GET requests. It is used to retrieve resources or data, without modifying the server state. This method should be safe and idempotent, meaning multiple calls return the same result and have no side effects. It’s commonly used to fetch a list of items, details of a single item, or paginated results.
  • @PostMapping Informs Spring that this method handles HTTP POST requests.
    It is primarily used to create new resources. The request body typically contains the details of the resource to be created. POST is not idempotent, meaning calling the same endpoint with the same data multiple times may result in multiple resource creations.

Leave a Reply

Your email address will not be published. Required fields are marked *