Whenever a client creates or updates data, it typically sends the details as a JSON document in the body of the request rather than in the URL. Spring Boot can turn that JSON straight into a Java object automatically, using a library called Jackson behind the scenes, so the incoming data can be worked with as ordinary objects rather than raw text.

Define a class for the data

public class Product {
  private String name;
  private double price;
  // getters and setters
}

Jackson matches JSON fields to Java fields by name, using the getters and setters to read and write values, which is why a class intended to receive JSON needs both, even though a plain object elsewhere in the code might not.

Accept it in a POST endpoint

The @RequestBody annotation tells Spring to read the JSON from the incoming request and build an object from it before the method even runs:

@PostMapping("/products")
public Product create(@RequestBody Product product) {
  // save product here
  return product;
}

What the client sends

{ "name": "Book", "price": 9.99 }

Spring reads this JSON and creates a Product object with those values already filled in, with no manual parsing required anywhere in the method.

Nested objects and lists

@RequestBody is not limited to flat objects. If a Product had a nested Category object, or the endpoint accepted a list of products, Jackson handles both automatically, as long as the Java classes mirror the structure of the JSON:

@PostMapping("/products/bulk")
public List<Product> createMany(@RequestBody List<Product> products) {
  return products;
}

Common mistakes

  • Field name mismatches: if the JSON key is productName but the Java field is name, the value silently ends up null rather than causing an error, which can be confusing to debug.
  • Missing a no-argument constructor: Jackson generally needs an empty constructor to create the object before filling in its fields, so a class with only a constructor that takes arguments may fail to deserialize.
  • Testing with a browser: typing a URL only sends GET requests, so POST endpoints need a tool such as Postman, or the curl command from a terminal, to actually send a JSON body.

Testing with curl

curl -X POST http://localhost:8080/products 
  -H "Content-Type: application/json" 
  -d '{"name":"Book","price":9.99}'

With @RequestBody in place, an API can accept structured input just as easily as it returns structured output, which is the second half of building a complete, working REST endpoint.