Spring Boot services · Chapter 38 of 41
Project: the orders service
By the end of this chapter you can build a whole service on your own: validated input, one transaction per use case, problem details for every error, and pages. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: Projects
- 6 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
From Monday the partner cafés at SBC, NDLS and CSMT send every order to Tapri Cloud, Meera says: no more paper bills. Ravi has one condition: "If the system says we sold six bun maska, we had six bun maska."
Each piece is a chapter you have done: DTOs (10), validation (12), error handling (13), paging (18), transactions (21), lazy loading (23). What goes wrong in a real service is how they meet: a stock change that is never written, half an order's stock gone, a lazy list read after its transaction closed, an error body the cafés' app cannot read.
What you build: 6 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
OrderService.java: place an order at menu prices
Write OrderService.place(request) and POST /orders. Make a new order for the request's station, placed at the clock's instant; for each line find the menu item by name, take the quantity off its stock and add a line at the item's current price. Save the order and answer 201 Created with a Location header /orders/<id> and the order as an OrderView: id, station, status PLACED, total, placedAt, and lines (item, quantity, unitPrice) in the request's order.
SBC with 2 Masala chai and 1 Bun maska gives total 130 and leaves Masala chai at 8 (from 10) and Bun maska at 2 (from 3) in the database. An item not on the menu (Filter coffee) throws UnknownItemException, which already answers 400, and saves no order.
- Task 2
OrderController.java: refuse a bad order with a problem detail
Validate the POST /orders body before the controller method runs: station not blank and 2 to 5 capital letters (SBC, never "bengaluru"); lines holding 1 to 10 lines; and in each line an item that is not blank and a quantity from 1 to 20.
A bad body answers 400 as a problem detail (application/problem+json) with status 400, title "Invalid order" and a property errors mapping each bad field's path to its message: "station", "lines" (an empty list), "lines[0].quantity" (0 or 21), "lines[1].item" (blank). No invalid order is saved.
- Task 3
OrderService.java: one order, or a 404 problem detail
Write OrderService.find(id) so GET /orders/{id} answers 200 with the order as an OrderView, lines included: NDLS with 3 Cutting chai has total 75 and its first line's item is Cutting chai.
An id that does not exist throws OrderNotFoundException and answers 404 as a problem detail (application/problem+json) with status 404, title "Order not found" and detail "No order <id>". The lines are lazy and open-in-view is off, so map the order inside a transaction.
- Task 4 · interview question
Interview: make placing an order all or nothing
Make place change nothing when an item runs out: a line asking for more than the item's stock throws OrderConflictException, no order is saved, and the stock taken for earlier lines is back. CSMT with 2 Masala chai (10 left) and 5 Bun maska (3 left) answers 409 as a problem detail with status 409 and title "Order conflict", and leaves Masala chai at 10, Bun maska at 3 and no order.
place must run in a transaction: the Check looks for the transaction annotation on place or on OrderService. Be ready to explain what happens to the first line's stock without it.
- Task 5
OrderService.java: a page of a station's orders, newest first
Write OrderService.page(station, page, size) for GET /orders?station=SBC&page=0&size=2: one page of that station's orders only, newest first (highest id first), as an OrderPage with items (OrderViews, lines included), page, size, totalElements and totalPages.
With SBC's three orders and size 2, page 0 holds the newest two, page 1 the oldest, and both say totalElements 3 and totalPages 2. A size above 50 (MAX_PAGE_SIZE) is capped: size 500 gives size 50.
- Task 6
OrderService.java: cancel an order once and put its stock back
Write OrderService.cancel(id) for POST /orders/{id}/cancel: a PLACED order becomes CANCELLED, each line's quantity goes back to its menu item's stock, and the answer is 200 with the OrderView (status CANCELLED). An order of 2 Masala chai takes the stock from 8 back to 10.
Cancelling it again throws OrderConflictException and gives nothing back: 409, title "Order conflict" (the handler from task 4). An id that does not exist answers 404.
What you will learn
- One transaction per use case: every write in it commits together or not at all.
- Map inside the transaction: entities never leave the service, so lazy lines are read while they still can be.
- Problem details: one JSON shape for every error, with a title the app can test.
- Capped pages: newest first, never more than a page can hold.
More in Projects
Build "Project: the orders service" 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.