Today we’re starting to get familiar with REST — a concept that’s long since taken over the web dev world. REST is so popular people joke it’s "JavaScript, but only about APIs." But let’s get to the point. We’ll dig into what REST is, why it’s useful, and how its principles are applied in modern apps.
What is REST?
REST (Representational State Transfer) is an architectural style for building web services, proposed by Roy Fielding in 2000. Think of REST like a set of traffic rules for the internet — it defines how apps should exchange data over the web so their interaction is efficient, reliable, and understandable. Just like traffic rules help cars move safely and efficiently on roads, REST sets clear principles for data exchange between client and server. These principles provide:
- Fast service performance
- Reliable data delivery
- The ability for the system to grow and evolve
- Ease of use for developers
Core principles of REST
At the heart of REST is the concept of resources. A resource is any entity you can manage: a user, an order, a product, or something else. Each resource has a unique identifier (URI) you can use to address it.
So:
- Resource-based: everything in REST is presented as resources. This is the key principle that defines how we organize data and interact with it.
Example:
/users/1 — information about the user with ID = 1 /orders/5 — details of the order with ID = 5 - HTTP methods are our best friends. REST relies on standard HTTP methods:
GET— retrieve data (read).POST— create a new resource.PUT— replace a resource.PATCH— update part of a resource.DELETE— remove a resource.
- Stateless. Every client-server interaction is independent — the server doesn’t store client state. If the client makes a request, it must provide all necessary data with that request.
- HTTP response codes. REST communicates results via response codes:
200 OK— Everything went well.201 Created— Resource created.404 Not Found— Resource not found.500 Internal Server Error— Something went wrong..
- Uniform Interface. REST helps ensure a standard way to access API resources. That means any client that understands the REST API protocol can talk to the server.
- Cacheable. The server can mark responses as cacheable to avoid repeating the same work for identical requests.
Benefits of using a REST API
Why did REST become so popular? It’s not just hype. REST has real benefits:
1. Universality
REST APIs can be used by any client: web apps, mobile apps, IoT devices, and even WiFi-enabled fridges.
2. Simplicity
REST runs on top of standard HTTP, which every developer already knows. No heavy libraries or exotic formats — just text-based requests and responses, usually JSON or XML.
3. Scalability
Thanks to being stateless, REST scales easily — requests are handled independently, so you can add more servers to handle load.
4. Language-agnostic
REST APIs can be implemented in any programming language, and clients can be written in another. The key is understanding the shared interface standards.
5. Easy integration
REST APIs are easy to integrate with frontend frameworks like React, Angular, or Vue.js, and with consumers on other platforms.
Analogy
Think of a REST API like a modern café. In it:
- The menu (API documentation) lists all available dishes (resources)
- Each dish has a menu number (a unique URI)
- There are standard actions: place an order (POST), get your order (GET), change the order (PUT) or cancel it (DELETE)
- The café doesn’t remember your past orders (stateless) — each time you place a new order with full info
- The kitchen may have prepped popular dishes (caching)
- You don’t care who cooks the dish — the head chef or the sous-chef (multi-tier system)
Typical use cases for REST
REST is used almost everywhere, from simple apps to complex microservice architectures:
- Web applications. REST APIs connect frontend and backend. For example, a shopper on an e-commerce site places an order through a REST API.
- Mobile apps. All those pretty apps you install on your phone use REST APIs to talk to servers.
- Microservices. In distributed systems, REST helps services communicate with each other.
- Third-party integrations. Payment gateways, authorization services (OAuth), email services — these are usually exposed as REST APIs.
Why REST and not something else?
You might ask: why REST specifically? Why not SOAP (yep, the ancient protocol), or GraphQL? SOAP, while powerful, requires a lot more setup and overhead. GraphQL, being flexible and modern, is harder to learn and better suited for specific cases. REST is the middle ground: standard, simple, and effective.
Example of using a REST API
Here’s how working with a REST API might look in practice. Say we have a User resource:
- Get a list of all users:
GET /users Response: 200 OK [ {"id": 1, "name": "Alice"}, {"id": 2, "name": "Bob"} ] - Get a user by ID:
GET /users/1 Response: 200 OK { "id": 1, "name": "Alice" } - Create a new user:
POST /users Body: { "name": "Charlie" } Response: 201 Created { "id": 3, "name": "Charlie" } - Update a user:
PUT /users/1 Body: { "name": "Alicia" } Response: 200 OK { "id": 1, "name": "Alicia" } - Delete a user:
DELETE /users/1 Response: 204 No Content
The simplicity of REST makes it an ideal choice for web app development. In the next lectures we’ll cover more advanced aspects of building REST APIs with Spring Boot, learn how to write CRUD APIs and handle requests. There are some cool practical examples and exercises ahead.
GO TO FULL VERSION