Sub topic 1
REST and HTTP in Brief: Resources, Methods, Status Codes
Prerequisites
This chapter assumes you know basic Java, including classes and records. It also assumes you have seen HTTP requests and responses before, and that you have used Maven or Gradle. You do not need Spring knowledge yet. This chapter gives the HTTP vocabulary that the rest of the tutorial relies on.
Resources and URLs
REST is a style for designing web APIs around resources. A resource is a thing your API exposes, such as a book. Each resource has a URL, and the client uses HTTP to act on it.
Two URL shapes cover most needs:
/api/booksis a collection of books./api/books/42is one book in that collection.
Use plural nouns for collections and keep verbs out of the path. The HTTP method already says what to do, so /api/books/42 is better than /api/getBook?id=42.
The server sends back a representation of the resource, usually JSON. A representation is a snapshot of the resource's state, not the object inside the server. In later chapters, DTO classes will describe these snapshots.
Methods and Their Guarantees
Each HTTP method has a meaning, and clients, caches, and proxies rely on it. Two properties matter most:
- A method is safe if it does not change server state.
- A method is idempotent if repeating the same request leaves the server in the same state as sending it once.
| Method | Typical use | Safe | Idempotent |
|---|---|---|---|
| GET | Read a resource or collection | Yes | Yes |
| POST | Create a resource, or run an action | No | No |
| PUT | Replace a resource completely | No | Yes |
| PATCH | Change part of a resource | No | Not guaranteed |
| DELETE | Remove a resource | No | Yes |
Idempotency matters when a network call fails and the client retries. Retrying a PUT or DELETE is safe. Retrying a POST can create a duplicate book.
Status Codes
The status code tells the client how the request went. Pick the code that matches the outcome, because clients branch on it.
| Code | Name | Use it when |
|---|---|---|
| 200 | OK | A read or update succeeded and the response has a body |
| 201 | Created | A new resource exists. Send a `Location` header with its URL |
| 204 | No Content | The request succeeded and there is no body, often after DELETE |
| 400 | Bad Request | The request is malformed or fails validation |
| 404 | Not Found | The resource does not exist |
| 409 | Conflict | The request clashes with current state, such as a duplicate |
| 500 | Internal Server Error | The server failed unexpectedly |
Codes are grouped by their first digit. 2xx means success, 4xx means the client made a mistake, and 5xx means the server failed. Do not return 200 with an error message in the body. Clients and monitoring tools read the status code first.
One Request, End to End
The sequence below shows a client creating a book and then fetching it.
Common Mistakes
- Using
POSTfor everything. This hides intent and breaks safe retries. - Putting verbs in URLs, such as
/api/deleteBook/42. - Returning 200 for failures.
- Returning 404 when the client's request body is invalid. Use 400 for that.
Where This Leads
Later chapters map each method to a Java controller method. They also set status codes with ResponseEntity and report errors in a consistent format. Every choice there comes back to the methods and codes in this chapter.
Exercise
A client sends PUT /api/books/42 with a full book body, and the connection drops before it sees a response. Is it safe to retry the request, and which status code should the server return when DELETE /api/books/42 succeeds with no body?
Show answer
Yes, a retry is safe because PUT is idempotent. Sending the same full replacement twice leaves the book in the same state. A successful DELETE with no body should return 204 No Content.
Exercise
A client posts a book with an empty title. Which status code fits, and why is returning 200 with an error message in the body a poor choice?
Show answer
Return 400 Bad Request, because the client sent invalid input. A 200 response tells clients, caches, and monitoring tools that the request succeeded, so they cannot detect the failure without parsing the body.