Spring Boot services · Chapter 9 of 41
Requests and responses
By the end of this chapter you can read a JSON request body, choose the status and headers of every answer, rename a JSON field, and explain which HTTP methods are safe and which are idempotent. 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
Partner cafés want to place orders through Tapri Cloud, not only read the menu. Café Konkan's developer sends the requirements: "We post JSON. When an order is created, say 201 and tell us where it lives. When there is nothing to send back, say 204. And our system reads snake_case."
So far your methods returned a value and Spring chose 200 for every answer. A plain return value cannot say "created", cannot add a header, and a void method answers 200 with an empty body, which tells the café nothing.
What you build: 5 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
OrderController.java: read a JSON order and answer 201 Created with a Location header
Make POST /orders read an OrderRequest from the JSON body. Look the item's price up in PRICES, store the order with store.add (total is price times quantity, ready in 2 + 2 x quantity minutes), and answer 201 Created with the stored order as the body (id, station, item, quantity, total and status PREPARING).
The Location header is the full URL of the new order, built from the current request: http://localhost:8081/orders/1 under run-app, http://localhost/orders/1 in the Check. An item that is not in PRICES (Pizza) or a quantity under 1 answers 400 Bad Request.
- Task 2
OrderController.java: return one order with its status in an X-Order-Status header, or 404
Make GET /orders/{id} answer 200 with the stored order as the body and its current status in a header named X-Order-Status (PREPARING, or PICKED_UP after a pickup).
An id that is not stored (987654) answers 404 Not Found.
- Task 3
OrderController.java: mark an order picked up and answer 204 No Content
Make POST /orders/{id}/pickup save the order with the status PICKED_UP and answer 204 No Content with an empty body. Keep the method void.
An id that is not stored (987654) answers 404 Not Found.
- Task 4
Order.java: send readyInMinutes as ready_in_minutes
Make Jackson write the record component readyInMinutes as "ready_in_minutes": an Order with readyInMinutes 4 is written with "ready_in_minutes":4 and no "readyInMinutes" anywhere in its JSON.
Keep the Java name readyInMinutes; only the JSON name changes.
- Task 5 · interview question
Interview: say which HTTP methods are safe and which are idempotent
Write HttpFacts.isSafe(method) and HttpFacts.isIdempotent(method) as the HTTP standard (RFC 9110) defines them, for GET, HEAD, OPTIONS, POST, PUT, PATCH and DELETE.
Safe means a request only reads. Idempotent means sending it twice leaves the server as sending it once does; count a method only when the standard guarantees this for every request with it. Be ready to explain both.
What you will learn
- @RequestBody: JSON in the request becomes a Java record.
- ResponseEntity and @ResponseStatus: you choose the status, the headers and the body.
- @JsonProperty: a JSON field name that differs from the Java one.
- Safe and idempotent: the interview question about HTTP methods.
More in REST APIs
Build "Requests and responses" 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.