Spring Boot services · Chapter 10 of 41

Layers and DTOs

By the end of this chapter you can split an API into a controller, a service and a repository, and answer with DTOs that say only what you decide. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: REST APIs
  • 5 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

A partner café sends Meera a screenshot of Tapri Cloud's answer to GET /menu/1:

$ curl localhost:8081/menu/1
{"id":1,"name":"Masala chai","price":40,"costPrice":12,"staffNote":"Assam blend, two cardamom pods a pot"}

"So your chai costs you twelve rupees," the owner writes, "and now I know your recipe."

Ravi's endpoint returns MenuItem, the class Tapri uses inside, and Jackson writes every field of whatever a controller returns. Hiding one field with an annotation fixes today's leak, but the next field someone adds to MenuItem leaks again. The menu's rules have the same problem: a check written inside the controller holds only for HTTP, and next month's import job skips it.

What you build: 5 tasks

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

  1. Task 1

    MenuService.java: add an item, keeping the menu's two rules

    Write MenuService.add(name, price, costPrice, staffNote). Trim the name, save a new MenuItem through the repository, and return the saved item with the id the repository gave it.

    Throw an IllegalArgumentException, and save nothing, when the price is below the cost price (equal is fine) or when an item with the same name is already on the menu, ignoring case ("masala CHAI" counts as Masala chai).

  2. Task 2

    MenuController.java: map an item to its public fields

    Write MenuItemResponse.from(item) so it builds a MenuItemResponse from the item's id, name and price, and nothing else.

    Keep the record's three fields exactly as they are: id, name and price, in that order.

  3. Task 3

    MenuController.java: list the menu at GET /menu

    Make GET /menu answer 200 with a JSON list of every menu item as a MenuItemResponse, in the order the service returns them (by id: Masala chai, then Cutting chai, then Bun maska).

    Get the items from the service, not the repository. No costPrice or staffNote may appear anywhere in the answer.

  4. Task 4

    MenuController.java: add an item with POST /menu

    Make POST /menu read a NewMenuItem from the JSON body and add it through the service, which keeps the staff note inside. Answer 201 Created with the new item as a MenuItemResponse (id, name and price only).

    When the service refuses the item (a loss-making price or a name already on the menu), answer 400 Bad Request instead of 500. The 400 needs no body.

  5. Task 5 · interview question

    Interview: stop GET /menu/{id} leaking the cost price and staff notes

    Make GET /menu/{id} answer 200 with the item as a MenuItemResponse: id, name and price only, with no costPrice or staffNote. An id that is not on the menu must still answer 404 Not Found.

    Be ready to explain what decides which fields a response sends.

What you will learn

  • Layers: the controller speaks HTTP, the service holds the rules, the repository stores; each one calls only the layer below it.
  • DTOs: small records made for one request or one response, so the API says exactly what you choose.
  • Mapping: turning a domain object into a DTO with one plain method.

More in REST APIs

Build "Layers and DTOs" 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.