REST stands for Representational State Transfer. It is an architectural style used for designing networked applications, especially web services. REST is a set of rules that define how clients (like browsers or apps) and servers communicate over the web. It’s commonly used when building APIs (like in Spring Boot) so that different systems can talk to each other using HTTP. REST is built on top of HTTP, and uses a request and response pattern.

+--------+                                +--------+
| Client |                                | Server |
+--------+                                +--------+
     |                                        ^
     |  HTTP Request (GET, POST, etc.)        |
     |--------------------------------------->|
     |                                        |
     |          HTTP Response (JSON, HTML)    |
     |<---------------------------------------|
     v                                        |

Key features

ConceptExplanation
ResourceAny object or data (like a user, post, product) that can be accessed. Each has a URL. Example: /users/123 is a resource for user with ID 123.
RepresentationHow the resource is formatted. REST typically uses JSON or XML to represent the resource.
StatelessEach request from the client must contain all the information needed; the server does not store anything about the client between requests.
Standard HTTP methodsREST uses the standard HTTP verbs to perform actions on resources

REST API conventions

Use pluralised nouns for resources

Use plural version of the noun to name your resource URL.

One book (using ID of that book):
/books/9789021

The collection of books:
/books

HTTP verbs

You can have the same URL but different HTTP verbs to execute different actions. For example GET /users/123 retrieves the user 123, POST /users/123 creates a new user 123.

HTTP VerbActionDescriptionExampleRequest body
GETReadRetrieves a representation of a resource without modifying it.GET /users/123 → Get user 123None
POSTCreateCreates a new resource on the server.POST /users → Create new userFull represetnation
PUTUpdate/ReplaceReplaces a resource entirely. You should provide the full representation of the resource in the request body.PUT /users/123 → Replace user 123Full represetnation
PATCHUpdate/ModifyPartially updates a resource. You don’t provide the full representation of the resource in the request body.PATCH /users/123 → Update name onlyPartial represetnation
DELETEDeleteRemoves the resource.DELETE /users/123 → Delete user 123None

HTTP response code in REST

HTTP response codes are standard 3-digit status codes sent by the server to indicate the outcome of an HTTP request. RESTful APIs use these codes to clearly communicate whether a request was successful, failed, or had some other outcome.

Code RangeMeaningUsage in REST APIs
1xxInformationalRarely used in REST
2xxSuccessThe request succeeded
3xxRedirectionUsually avoided in REST unless for location redirects
4xxClient errorSomething wrong with the request
5xxServer errorServer failed to process a valid request

Common HTTP codes in REST

Success codes (2xx)

CodeMeaningREST Use Case
200 OKRequest succeededStandard response for GET, PUT, or DELETE
201 CreatedResource createdResponse to successful POST
204 No ContentSuccess, no response bodyUsed for DELETE or successful update with no body

Client error code (4xx)

CodeMeaningREST Use Case
400 Bad RequestInvalid request dataMissing fields, wrong format, etc.
401 UnauthorizedNot authenticatedClient must log in (e.g., missing token)
403 ForbiddenAuthenticated but no permissionAccess denied to the resource
404 Not FoundResource doesn’t existWrong URL or ID
409 ConflictResource conflicte.g., duplicate entry on POST
422 Unprocessable EntitySemantically invalid datae.g., failed validation

Server error code (5xx)

CodeMeaningREST Use Case
500 Internal Server ErrorGeneric server errorUnexpected failure during processing
503 Service UnavailableServer is down or overloadedTemporary maintenance or overload

Query parameter and URL path

Query parameter

Use query parameters when you need additional information in your request, but it doesn’t make sense to put it in the URL path. It is a key-value pair appended to the end of URL to send additional data. Often used for filtering, searching, sorting, or paginating.

Basic syntax: Query parameters are added after a ? in the URL. Multiple parameters are separated by &.

https://example.com/resource?key1=value1&key2=value2

Examples in a REST API

Example 1: Filtering Fetch all users with the role “admin”.

GET /users?role=admin

Example 2: Searching Search for products with “pepsi” in the name or description

GET /products?search=pepsi

Example 3: Pagination

GET /posts?page=2&limit=10

Back to parent page: Spring and Spring Boot