Mapping HTTP Methods (GET, POST, PUT, DELETE) to Controller Methods

In a RESTful application, HTTP methods are used to perform different operations on resources. These methods include GET, POST, PUT, and DELETE, and they are mapped to corresponding controller methods to handle the requests.

GET Method

The GET method is used to retrieve information from the server. It should be safe and idempotent, meaning that executing the same request multiple times should have the same result. In Spring Boot, we can map the GET method to a controller method using the @GetMapping annotation.

For example, the following code snippet maps a GET request for /products to the getProducts() method in the ProductController:

@RestController
public class ProductController {

    @GetMapping("/products")
    public List<Product> getProducts() {
        // Code to retrieve products from the database
        // ...
        return products;
    }

}

POST Method

The POST method is used to submit data to be processed by the server. It is not idempotent and can have side effects, such as creating a new resource. In Spring Boot, we can map the POST method to a controller method using the @PostMapping annotation.

For example, the following code snippet maps a POST request for /products to the createProduct() method in the ProductController:

@RestController
public class ProductController {

    @PostMapping("/products")
    public ResponseEntity<String> createProduct(@RequestBody Product product) {
        // Code to create a new product in the database
        // ...
        return ResponseEntity.ok("Product created successfully");
    }

}

PUT Method

The PUT method is used to update an existing resource or create a new resource if it doesn't exist already. Like the POST method, it is not idempotent and can have side effects. In Spring Boot, we can map the PUT method to a controller method using the @PutMapping annotation.

For example, the following code snippet maps a PUT request for /products/{productId} to the updateProduct() method in the ProductController:

@RestController
public class ProductController {

    @PutMapping("/products/{productId}")
    public ResponseEntity<String> updateProduct(@PathVariable Long productId, @RequestBody Product product) {
        // Code to update the product in the database
        // ...
        return ResponseEntity.ok("Product updated successfully");
    }

}

DELETE Method

The DELETE method is used to remove a resource from the server. It is idempotent, meaning that executing the same request multiple times should have the same result. In Spring Boot, we can map the DELETE method to a controller method using the @DeleteMapping annotation.

For example, the following code snippet maps a DELETE request for /products/{productId} to the deleteProduct() method in the ProductController:

@RestController
public class ProductController {

    @DeleteMapping("/products/{productId}")
    public ResponseEntity<String> deleteProduct(@PathVariable Long productId) {
        // Code to delete the product from the database
        // ...
        return ResponseEntity.ok("Product deleted successfully");
    }

}

By mapping the appropriate HTTP methods to controller methods, we can handle different types of requests in our Spring Boot application and perform the necessary operations on the resources.


noob to master © copyleft