REST APIsCore· 40 min read

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.

GETPOST
PurposeRead / fetch dataSend / create data
Has a body?NoYes (usually JSON)
Spring annotation@GetMapping@PostMapping
Typical useList productsAdd 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:

A POST endpoint reading the request body into a Product
@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:

Sending a POST request with 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:

  1. You (or a front-end) send a POST request to /products with a JSON body, e.g. {"id":3,"name":"Monitor","price":12999.0}.
  2. Spring sees the method is POST and the path is /products, so it picks the method marked @PostMapping("/products").
  3. 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).
  4. Your method runs with that ready-made product object, so product.getName() gives "Monitor".
  5. 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?

Answer: @RequestBody tells Spring to read the JSON sent by the client and turn it into the Java object parameter, so you can work with the data as a normal object.

✍️ Practice

  1. Add a POST /books endpoint that takes a Book in the body and returns “Saved: <title>”.
  2. Test it with Postman or curl by sending a JSON body.

🏠 Homework

  1. Build a POST /students endpoint that reads a student from the body and returns a confirmation message with the name and course.
Want to learn this with a mentor?

CodingClave runs guided, project-based training (28-day, 45-day & 6-month batches).

Explore Training →