Spring Boot @PutMapping Example

1. Introduction

The @PutMapping annotation in Spring Boot is used for mapping HTTP PUT requests onto specific handler methods. Typically used for update scenarios, it is a specialized version of the @RequestMapping method that is focused solely on the PUT action.

Key Points:

1. @PutMapping is specifically designed to handle HTTP PUT requests.

2. It is ideal for RESTful web services, where PUT typically signifies an update operation.

3. Similar to @PostMapping and @GetMapping, it is used to make the controller methods more readable and explicitly designates the action they perform.

2. Implementation Steps

1. Start by creating a Spring Boot application with web dependencies.

2. Define a domain model class to represent the data structure.

3. Create a REST controller class with a method annotated with @PutMapping to handle PUT requests.

4. Define a URL endpoint that the @PutMapping method will listen to for incoming requests.

5. Implement the logic to update the model within the handler method.

6. Test the PUT endpoint using a REST client or a tool like cURL or Postman.

3. Implementation Example

Here is the complete code that demonstrates the usage of @PutMapping annotation:
// Step 2: Define a simple domain model class
class Book {
    private Long id;
    private String title;
    private String author;

    // Constructors, Getters, and Setters here
    // ...
}

// Step 3: Create a REST controller to handle book updates
@RestController
public class BookController {

    @PutMapping("/books/{id}")
    public ResponseEntity<Book> updateBook(@PathVariable Long id, @RequestBody Book book) {
        // Step 5: Normally you would update the book in the database
        // This example simply returns the updated book for demonstration
        book.setId(id);
        return new ResponseEntity<>(book, HttpStatus.OK);
    }
}

// Step 6: Define the main application class
@SpringBootApplication
public class PutMappingExampleApplication {

    public static void main(String[] args) {
        SpringApplication.run(PutMappingExampleApplication.class, args);
    }
}

Output:

{
    "id": 1,
    "title": "Updated Book Title",
    "author": "Author Name"
}

Explanation:

1. The Book class represents the domain model with attributes like id, title, and author.

2. The BookController is a @RestController, meaning it handles HTTP requests in a RESTful manner.

3. @PutMapping("/books/{id}") is used to map HTTP PUT requests for a specific book ID to the updateBook method.

4. @PathVariable Long id extracts the id from the URL, while @RequestBody Book book deserializes the request body into a Book object.

5. In a real scenario, updateBook would contain logic to update the book resource in a database. Here, it simply returns the passed Book object as if it were updated, along with an HTTP 200 OK status.

6. The PutMappingExampleApplication class contains the main method that boots up the Spring Boot application.

7. When a PUT request is sent to /books/1 with the appropriate JSON body, the output is a JSON representation of the updated Book, indicating a successful mock update.


Comments