AI Hints
Home›Spring Boot Tutorial›@PostMapping and @RequestBody, @PutMapping and @DeleteMapping
Spring Boot · Lesson 22 of 120

@PostMapping and @RequestBody

Creating data.

⏱ 2 min read▶ Video at 29:08🎓 Free
Watch this lesson in the Spring Boot courseWatch on YouTube ↗Full Spring Boot course

So far, our API only reads data. Now, clients will send us new books, as JSON, in the body of a POST request.

  • So far, only reading
  • Now, clients send data
  • JSON in the body

A POST request

The client sends POST slash books, with JSON in the body. A header says the body is JSON. Spring reads the body, and Jackson turns the JSON into a Book record. Our method receives the record, and saves it. And we send back the saved book, with its new id.

  1. POST /books + JSON body
  2. Content-Type: application/json
  3. Jackson: JSON → Book
  4. Our method saves it
  5. Response: saved book

Adding books

We keep books in a map, in memory, keyed by id. Next id counts up, from one. Get Mapping with no path, answers slash books itself. At Post Mapping handles POST. And at Request Body says, fill this book from the JSON body. We give it the next id, store it, and return it. With curl, dash dash json sends JSON, as a POST, with the right header. Run it. Dune gets id one, and Circe gets id two. Then GET lists both books.

BookController.javajava
@RestController
@RequestMapping("/books")
public class BookController {

    private final Map<Integer, Book> books = new LinkedHashMap<>();
    private int nextId = 1;

    @GetMapping
    public Collection<Book> all() {
        return books.values();
    }

    @PostMapping
    public Book add(@RequestBody Book book) {
        Book saved = new Book(nextId++, book.title(), book.author(), book.price());
        books.put(saved.id(), saved);
        return saved;
    }
}
Output
$ curl --json '{"title":"Dune","author":"Herbert","price":9.99}' localhost:8080/books
{"id":1,"title":"Dune","author":"Herbert","price":9.99}
$ curl --json '{"title":"Circe","author":"Miller","price":10.99}' localhost:8080/books
{"id":2,"title":"Circe","author":"Miller","price":10.99}
$ curl localhost:8080/books
[
  {"id":1,"title":"Dune","author":"Herbert","price":9.99},
  {"id":2,"title":"Circe","author":"Miller","price":10.99}
]
Also uses Book.java
public record Book(Integer id, String title, String author, double price) {}
Also uses _curl.sh
curl --json '{"title":"Dune","author":"Herbert","price":9.99}' localhost:8080/books
curl --json '{"title":"Circe","author":"Miller","price":10.99}' localhost:8080/books
curl localhost:8080/books

Bad bodies

What if the client sends something wrong? First, broken JSON. Second, form data instead of JSON. Run it. Broken JSON is a bad request, four hundred. And form data gets four one five, unsupported media type, because we only accept JSON.

BookController.javajava
@RestController
@RequestMapping("/books")
public class BookController {

    private final Map<Integer, Book> books = new LinkedHashMap<>();
    private int nextId = 1;

    @GetMapping
    public Collection<Book> all() {
        return books.values();
    }

    @PostMapping
    public Book add(@RequestBody Book book) {
        Book saved = new Book(nextId++, book.title(), book.author(), book.price());
        books.put(saved.id(), saved);
        return saved;
    }
}
Output
$ curl -i --json '{"title":"Dune", "price":}' localhost:8080/books | head -1
HTTP/1.1 400
$ curl -i -X POST -d 'title=Dune' localhost:8080/books | head -1
HTTP/1.1 415
Also uses Book.java
public record Book(Integer id, String title, String author, double price) {}
Also uses _curl.sh
curl -i --json '{"title":"Dune", "price":}' localhost:8080/books | head -1
curl -i -X POST -d 'title=Dune' localhost:8080/books | head -1

Summary

At Post Mapping handles POST, and at Request Body turns the JSON body into a Java object. Next, updating books with PUT.

Key takeaways
  • @PostMapping
  • @RequestBody Book book
  • curl --json
Next up: @PutMapping — updating data