0
0
Spring Bootframework~30 mins

@PutMapping and @DeleteMapping in Spring Boot - Mini Project: Build & Apply

Choose your learning style9 modes available
Using @PutMapping and @DeleteMapping in Spring Boot
📖 Scenario: You are building a simple Spring Boot REST API to manage a list of books in a library. You want to allow users to update book details and delete books using HTTP methods.
🎯 Goal: Create a Spring Boot controller with endpoints to update a book's information using @PutMapping and delete a book using @DeleteMapping.
📋 What You'll Learn
Create a Book class with id, title, and author fields.
Create a BookController class with a list of books as initial data.
Add a @PutMapping method to update a book by id.
Add a @DeleteMapping method to delete a book by id.
💡 Why This Matters
🌍 Real World
Updating and deleting resources via REST APIs is common in web applications like library systems, inventory management, and user profiles.
💼 Career
Understanding @PutMapping and @DeleteMapping is essential for backend developers working with Spring Boot to build RESTful services.
Progress0 / 4 steps
1
Create initial book data
Create a BookController class with a List<Book> called books initialized with these exact books: new Book(1, "1984", "George Orwell") and new Book(2, "Brave New World", "Aldous Huxley").
Spring Boot
Need a hint?

Use List.of() inside new ArrayList<>() to create the initial list.

2
Add a configuration variable for book count
Inside BookController, add a private integer variable called nextId and set it to 3 to track the next book ID.
Spring Boot
Need a hint?

Declare nextId as a private integer and assign it the value 3.

3
Add @PutMapping method to update a book
Add a method annotated with @PutMapping("/books/{id}") named updateBook that takes @PathVariable int id and @RequestBody Book updatedBook. Use a for loop with variables book and update the matching book's title and author if book.getId() == id. Return the updated book.
Spring Boot
Need a hint?

Use @PutMapping with path /books/{id}. Loop through books to find the book with matching id and update its fields.

4
Add @DeleteMapping method to delete a book
Add a method annotated with @DeleteMapping("/books/{id}") named deleteBook that takes @PathVariable int id. Use a for loop with index i to find the book with matching id and remove it from books. Return void.
Spring Boot
Need a hint?

Use @DeleteMapping with path /books/{id}. Loop with index to find and remove the book.