Chapter 3.3☕ 14 min read

POST Mapping and RequestBody

POST creates. @RequestBody receives. Jackson converts.

01The Concept: Request Body

The Interview Form Analogy:

Imagine you go to an HR desk at a Hyderabad IT firm to fill out your joining details. You take a form, write down your name, email, and address, and hand the entire form back to HR. You don't shout individual pieces of information at them one by one: "Name is Ramesh! Email is ramesh@x.com!". You submit one complete form (an object) all at once.

In a REST API, when the frontend (Angular/React) sends new data to the backend, it doesn't put it in the URL. It builds a JSON object and sends it inside the HTTP Request's "Body". @RequestBody tells Spring: "Take the JSON that arrived in the body and convert it into a Java object."

02Technical Explanation
  1. @PostMapping: Tells Spring to handle HTTP POST requests. Unlike GET, POST requests have a body.
  2. @RequestBody: Triggers Spring's Jackson library. It takes the incoming JSON (e.g., {"title":"New Book","author":"XYZ"}) and maps it to a Java class (Book).
  3. Rules for Mapping: JSON keys must exactly match Java variable names. The Java class must have a default (no-arg) constructor and Getters/Setters.
03Full Working Code: Creating a Resource
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, "Hyderabad History", "Sai")
));

// 1. Handle POST requests to /api/books
@PostMapping
public String createBook(@RequestBody Book newBook) {
// 2. Spring converts incoming JSON to a Book object automatically
newBook.setId(generateNewId()); // Give it a new ID
books.add(newBook); // Save to our fake DB

return "Book created successfully with ID: " + newBook.getId();
}

private int generateNewId() {
return books.get(books.size() - 1).getId() + 1;
}

@GetMapping
public List getAllBooks() {
return books;
}
}

// Notice we added setters now, so Jackson can set the values from JSON
class Book {
private int id;
private String title;
private String author;

// Default constructor is required for Jackson
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 POST with Postman

To test this, you cannot use your browser (browsers only do GET). You must use Postman or VS Code Thunder Client:

  1. Method: POST
  2. URL: http://localhost:8080/api/books
  3. Body (raw JSON): {"title": "Microservices Guide", "author": "Deva"}

When you hit send, Spring reads the JSON, creates a Book object, and adds it to the list!

05Why It Matters / Interview Note

Interview Question: "How does Spring convert JSON to Java objects?"

Answer: Spring Boot auto-configures the Jackson HTTP message converter. When a POST request arrives with Content-Type: application/json, Jackson uses reflection to read the JSON keys, matches them to Java class fields, and calls the corresponding setter methods to populate the object.

Enterprise Note: Never trust incoming @RequestBody data. In Phase 5, we will use @Valid and Bean Validation (@NotNull, @Size) to ensure the client didn't send an empty title or an invalid email in the body before we process it.

Key Takeaways

  • ✅ @PostMapping handles HTTP POST requests for creating resources
  • ✅ @RequestBody converts incoming JSON to Java objects via Jackson
  • ✅ Java class needs a default constructor and setters for Jackson to work
  • ✅ Always validate incoming data — never trust the client