@PostMapping vs @PutMapping in Spring Boot

In this quick guide, let's understand @PostMapping and @PutMapping annotations, examples, use cases, and differences.

What is Spring Boot?

Spring Boot is a project built on top of the Spring framework. It offers a way to quickly start and create stand-alone, production-grade Spring-based applications with minimal fuss. It abstracts away many of the complex configurations that Spring requires, making it easier and faster to deploy and develop applications.

HTTP POST vs HTTP PUT

Before deep diving into the annotations, it’s important to understand the difference between POST and PUT HTTP methods:

POST is typically used to create a new resource on the server. It is not idempotent, meaning multiple requests can have different effects.

PUT is used to update an existing resource or create a new one if it does not exist, based on the provided ID. It is idempotent, which means that making multiple identical requests will always produce the same result.

Using @PostMapping in Spring Boot

@PostMapping is a composed annotation that acts as a shortcut for @RequestMapping(method = RequestMethod.POST). It is designed to map HTTP POST requests onto specific handler methods.

It is typically used for creating new resources where the exact URL of the resource is not known in advance. POST is non-idempotent, meaning that multiple identical requests might have different effects (e.g., creating multiple entries).

Example of @PostMapping

Imagine you are developing an application where you need to add new products to your inventory. Here’s how you might use @PostMapping to handle this:
@RestController
@RequestMapping("/products")
public class ProductController {

    @PostMapping
    public ResponseEntity<Product> addProduct(@RequestBody Product product) {
        Product savedProduct = productService.saveProduct(product);
        return ResponseEntity.status(HttpStatus.CREATED).body(savedProduct);
    }
}

Here, any POST request to /products will trigger the addProduct method, creating a new product in the database.

Using @PutMapping in Spring Boot

@PutMapping acts as a shortcut for @RequestMapping(method = RequestMethod.PUT) annotation, and it is used to map incoming HTTP PUT requests to a specific handler method.

It is generally used to update existing resources or create a resource at a specific URL if it doesn't already exist. PUT is idempotent, meaning multiple identical requests should result in the same state (e.g., updating a resource won't create multiple instances of the resource).

Example of @PutMapping

Suppose you want to update the details of an existing product. Here’s how @PutMapping can be implemented:

@RestController
@RequestMapping("/products")
public class ProductController {

    @PutMapping("/{id}")
    public ResponseEntity<Product> updateProduct(@PathVariable Long id, @RequestBody Product product) {
        Product updatedProduct = productService.updateProduct(id, product);
        return ResponseEntity.ok(updatedProduct);
    }
}

Summary: Differences

@PostMapping and @PutMapping are annotations in Spring Boot used to handle different types of HTTP requests within RESTful web services:
  • @PostMapping annotation is used to map HTTP POST requests. It is typically used for creating new resources where the exact URL of the resource is not known in advance. POST is non-idempotent, meaning multiple identical requests might have different effects (e.g., creating multiple entries).
  • @PutMapping annotation is used to map HTTP PUT requests. It is generally used for updating existing resources or creating a resource at a specific URL if it doesn't already exist. PUT is idempotent, meaning multiple identical requests should result in the same state (e.g., updating a resource won't create multiple instances of the resource).

Comments