Spring Boot services · Chapter 11 of 41

Updating and deleting

By the end of this chapter you can change a resource with PUT and PATCH, remove it with DELETE, and answer the right status every time. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: REST APIs
  • 4 tasks
  • Spring Boot
  • In the all labs plan

The first 2 chapters of Spring Boot services are free. This one comes with the all labs plan.

Why you need this

It is the first week of the monsoon, and Ravi changes the menu every hour: Cutting chai goes up to 30 rupees because milk is late, Bun maska is sold out by 9 am, and the winter special comes off the menu. "I need to change one price without retyping the whole item, and when something is gone, it should stay gone."

So far the menu API can create and read. To change a price, Ravi would delete the item and add it again, and the new item gets a new id, so every café link to the old one breaks. One endpoint per field (/menu/2/price) does not scale either.

What you build: 4 tasks

Each task has tests. You write the code in the lab's editor and press Check.

  1. Task 1

    MenuController.java: replace a whole menu item with PUT /menu/{id}

    Make PUT /menu/{id} replace every field of the stored item with the ItemRequest body, save it in the store, and answer 200 with the saved item. The id comes from the path, never from the body.

    A field left out of the body is not kept: PUT /menu/1 with {"id": 99, "name": "Masala chai (large)", "price": 60, "available": false} stores item 1 as Masala chai (large), 60, not available, with description null, and nothing under id 99.

  2. Task 2

    MenuController.java: change only the fields sent with PATCH /menu/{id}

    Write ItemPatch.applyTo(item) so it returns a new MenuItem with the item's id where each field is the patch's value when it is not null, and the item's value otherwise.

    Make PATCH /menu/{id} apply the patch to the stored item, save the result and answer 200 with it: PATCH /menu/2 with {"price": 30} gives Cutting chai at 30, still available, with its description; a second PATCH with {"available": false} keeps the price 30 and makes it not available.

  3. Task 3

    MenuController.java: answer 404 to a PUT or PATCH for an item that does not exist

    PUT /menu/42 and PATCH /menu/42, for an id that is not in the store, both answer 404 Not Found and create nothing: afterwards there is still no item 42 and the menu has its 3 items.

  4. Task 4

    MenuController.java: delete an item with DELETE /menu/{id}, answering 204 and then 404

    Make DELETE /menu/{id} remove the item from the store and answer 204 No Content with an empty body. When there is no such item (any more), answer 404 Not Found.

    DELETE /menu/3 answers 204, then GET /menu/3 and a second DELETE /menu/3 answer 404, and the other two items stay on the menu.

What you will learn

  • PUT: replace the whole resource with the one sent.
  • PATCH: change only the fields that were sent.
  • DELETE: 204 No Content, then 404, and why that is still idempotent.

More in REST APIs

Build "Updating and deleting" yourself

Tapri Cloud: the counters’ Java grows into a Spring Boot service with a database. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.

The first 2 chapters of Spring Boot services are free. This one comes with the all labs plan.