Chapter 3.4☕ 14 min read

PUT Mapping

PUT replaces the entire resource. PATCH updates specific fields.

01The Concept: PUT vs PATCH

The Resume Update Analogy:

You have a printed resume in your hand.

  • PATCH (Partial Update): You take a pen, cross out the old phone number, and write the new one. The rest of the resume remains exactly the same.
  • PUT (Full Replace): You tear up the entire resume and print a brand new one. Even if only the phone number changed, an entirely new file is created.

In a REST API:

  • PUT expects the client to send the entire updated object. Any fields not sent will be set to null.
  • PATCH expects only the specific fields that changed.

Note: Real-world enterprise apps mostly use PUT for simplicity unless the objects are massive. We will cover PUT here.

02Technical Explanation
  1. @PutMapping("/{id}"): Handles PUT requests. It needs a path variable to know which item to update.
  2. Logic: We find the existing object in our DB (list), replace its fields with the new data from @RequestBody, and save it back.
03Full Working Code: Full Update
package com.devinhyderabad;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.*;
import java.util.ArrayList;
import java.util.List;

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

@RestController
@RequestMapping("/api/books")
class BookController {

private List books = new ArrayList<>(List.of(
new Book(101, "Old Title", "Sai")
));

// 1. Handle PUT requests to /api/books/101
@PutMapping("/{id}")
public String updateBook(@PathVariable int id, @RequestBody Book updatedBook) {

// 2. Find the existing book
for (Book book : books) {
if (book.getId() == id) {
// 3. Replace fields with new data
book.setTitle(updatedBook.getTitle());
book.setAuthor(updatedBook.getAuthor());
return "Book updated successfully!";
}
}

return "Book not found!";
}
}

class Book {
private int id;
private String title;
private String author;

public Book() {}
public Book(int id, String title, String author) {
this.id = id;
this.title = title;
this.author = author;
}

public int getId() { return id; }
public void setId(int id) { this.id = id; }
public String getTitle() { return title; }
public void setTitle(String title) { this.title = title; }
public String getAuthor() { return author; }
public void setAuthor(String author) { this.author = author; }
}
04Testing PUT with Postman

To test in Postman:

  1. Method: PUT
  2. URL: http://localhost:8080/api/books/101
  3. Body (raw JSON): {"title": "New Updated Title", "author": "Deva"}
05Why It Matters / Interview Note

Interview Question: "When should I use PUT vs PATCH?"

Answer: Use PUT when you are replacing the entire resource. The client must send all fields, even if they haven't changed. Use PATCH when you want to update only one or two specific fields, leaving the rest untouched. PATCH is more network-efficient for large objects but harder to implement safely on the backend due to merging logic.

Enterprise Note: A common trap in enterprise code is using PUT but forgetting to update all fields. If a Book had a publishedYear field, and the client didn't send it in the PUT body, it would become null or 0 in the database. This is why PUT requires strict API contracts.

Key Takeaways

  • ✅ @PutMapping handles HTTP PUT requests for full resource replacement
  • ✅ PUT requires @PathVariable to identify which resource to update
  • ✅ PUT + @RequestBody = client sends the entire updated object
  • ✅ PATCH is for partial updates — PUT replaces everything