Sub topic 1
REST and CRUD: mapping operations to HTTP
Prerequisites
This chapter assumes you know:
- Java basics: classes, records, interfaces, annotations.
- HTTP basics: methods, status codes, JSON bodies.
- Relational database and SQL basics.
- Maven or Gradle at a user level.
No Spring code is written yet. This chapter sets the vocabulary for the API you build in the rest of the tutorial.
The working model
CRUD stands for create, read, update, and delete, the four things you do with stored data. REST is a style for web APIs in which every thing the API manages is a resource. A resource has a URL, and the HTTP method says what to do with it.
In this tutorial the resource is a book. The collection lives at /api/books, and one book lives at /api/books/{id}. URLs name nouns, in the plural. The method supplies the verb, so you will not see paths like /api/createBook.
| CRUD operation | HTTP method | URL | Success status |
|---|---|---|---|
| Create | POST | `/api/books` | 201 Created, with a `Location` header |
| Read one | GET | `/api/books/{id}` | 200 OK |
| Read many | GET | `/api/books` | 200 OK |
| Update | PUT | `/api/books/{id}` | 200 OK |
| Delete | DELETE | `/api/books/{id}` | 204 No Content |
Safe and idempotent methods
Two properties of HTTP methods decide how clients and proxies treat your endpoints.
- A safe method does not change server state. GET is safe.
- An idempotent method has the same effect whether you call it once or many times. GET, PUT, and DELETE are idempotent. POST is not.
Retrying a PUT with the same body leaves the book in the same state. Retrying a POST creates a second book. Keep this in mind when you decide which method an operation uses.
PUT replaces the whole resource. The client sends every field, and the server overwrites the stored values. Partial updates normally use PATCH, which this tutorial does not cover.
One request, end to end
Here is a create request and its response. Creating a book and reading it back looks like this on the wire:
POST /api/books HTTP/1.1
Host: localhost:8080
Content-Type: application/json
{"title": "Dune", "author": "Frank Herbert"}HTTP/1.1 201 Created
Location: http://localhost:8080/api/books/1
Content-Type: application/json
{"id": 1, "title": "Dune", "author": "Frank Herbert"}The server picked the id, and the Location header tells the client where the new resource lives. The client never chooses the id on a POST.
Status codes for failures
Success codes are only half the contract. Clients rely on error codes to decide what to do next.
- 400 Bad Request: the body is malformed or fails validation.
- 404 Not Found: no resource has that id.
- 409 Conflict: the request clashes with current state, such as a duplicate unique value.
- 500 Internal Server Error: a bug or an unhandled failure on the server.
Later chapters implement 400 and 404 with validation and a central exception handler.
Common mistakes
- Using GET for actions that change data. Browsers, caches, and crawlers assume GET is safe.
- Returning 200 with an error message in the body. Clients that check only the status code will treat the failure as success.
- Putting verbs in URLs, such as
/api/deleteBook/3, instead of usingDELETE /api/books/3. - Returning 200 with an empty body for a DELETE. Use 204, which says explicitly that there is no body.
- Exposing database internals in the JSON. The chapter on DTOs shows how to keep the contract separate from the table.
Exercise
A client sends the same PUT request to /api/books/7 three times because of network retries. Another client sends the same POST to /api/books three times. What is the state of the data afterward in each case, and why?
Show answer
After three identical PUTs, book 7 holds the values from the request, exactly as after one PUT, because PUT is idempotent. After three POSTs, there are three new books with different ids, because POST is not idempotent and each call creates a resource.