POST with a Request Body
A POST endpoint receives data sent by the client — Spring turns the incoming JSON into a Java object for you.
What you will learn
- Create a @PostMapping endpoint
- Read the request body with @RequestBody
- Understand GET vs POST
GET reads, POST creates
A GET request asks for data (show me the products). A POST request sends data to be saved (here is a new product). For POST you use @PostMapping, and you read the sent data with @RequestBody.
| GET | POST | |
|---|---|---|
| Purpose | Read / fetch data | Send / create data |
| Has a body? | No | Yes (usually JSON) |
| Spring annotation | @GetMapping | @PostMapping |
| Typical use | List products | Add a product |
Receive JSON as an object
When the client sends JSON, @RequestBody tells Spring to convert that JSON into a Java object — the reverse of last lesson:
@RestController
public class ProductController {
@PostMapping("/products")
public String create(@RequestBody Product product) {
// here you would save it to a database (Unit 3)
return "Created: " + product.getName()
+ " (price " + product.getPrice() + ")";
}
}Note: Output:
Client sends this JSON body to POST /products:
{"id":3,"name":"Monitor","price":12999.0}
Response:
Created: Monitor (price 12999.0)
@RequestBody turned the incoming JSON into a Product object, so product.getName() works straight away.
How to test a POST
You cannot send a POST by just typing a URL in the browser (that sends a GET). Use a tool like Postman, the VS Code REST Client, or curl:
curl -X POST http://localhost:8080/products \
-H "Content-Type: application/json" \
-d "{\"id\":3,\"name\":\"Monitor\",\"price\":12999.0}"Note: Output:
Created: Monitor (price 12999.0)
The -X POST sets the method, -H sets the content type to JSON, and -d carries the JSON body.
The whole POST flow, step by step
Here is the complete journey of one POST request, from your tool to the response — read it in order so you can picture exactly what happens:
- You (or a front-end) send a POST request to
/productswith a JSON body, e.g.{"id":3,"name":"Monitor","price":12999.0}. - Spring sees the method is POST and the path is
/products, so it picks the method marked@PostMapping("/products"). - Because the parameter has
@RequestBody, Spring reads the JSON body and converts it into a Product object (it matches the JSON keys to the field names). - Your method runs with that ready-made
productobject, soproduct.getName()gives"Monitor". - The method returns a value (here a text message), and Spring sends it back to the client as the response.
Tip: A handy convention: a POST to /products creates a product, while a GET to /products lists them. Same URL, different method, different job — that is the REST style.
Watch out: For @RequestBody to fill your object, the JSON keys must match the field names, and the class needs a suitable constructor or setters. Mismatched names leave those fields empty.
Q. What does @RequestBody do on a POST method parameter?
✍️ Practice
- Add a POST
/booksendpoint that takes a Book in the body and returns “Saved: <title>”. - Test it with Postman or curl by sending a JSON body.
🏠 Homework
- Build a POST
/studentsendpoint that reads a student from the body and returns a confirmation message with the name and course.