REST API Design
REST is the standard way for systems to communicate over HTTP. Every mobile app, frontend, and microservice talks to a backend through REST APIs. This is the skill that gets you hired.
REST (Representational State Transfer) is an architectural style for building web APIs. A REST API uses HTTP protocol and follows these constraints:
- Client-Server โ frontend and backend are separate. Frontend calls API, doesn't know how backend works.
- Stateless โ each request contains all the information needed. Server doesn't remember previous requests. (No sessions by default.)
- Uniform Interface โ standard HTTP methods (GET, POST, PUT, DELETE) and standard formats (JSON).
- Resource-based URLs โ URLs represent things (nouns), not actions (verbs).
Read data. Never changes state. Safe and idempotent.
Create new resource. Not idempotent.
Replace entire resource. Idempotent.
Update part of a resource.
Remove a resource. Idempotent.
| Action | HTTP Method | URL | Request Body |
|---|---|---|---|
| Get all students | GET | /api/students | None |
| Get one student | GET | /api/students/1 | None |
| Create student | POST | /api/students | JSON student data |
| Replace student | PUT | /api/students/1 | Complete JSON |
| Update email only | PATCH | /api/students/1 | Only changed fields |
| Delete student | DELETE | /api/students/1 | None |
| Code | Name | When to use |
|---|---|---|
| 200 | OK | Successful GET, PUT, PATCH |
| 201 | Created | Successful POST (resource created) |
| 204 | No Content | Successful DELETE (nothing to return) |
| 400 | Bad Request | Invalid request data (validation failed) |
| 401 | Unauthorized | Not authenticated (no/invalid token) |
| 403 | Forbidden | Authenticated but not allowed |
| 404 | Not Found | Resource doesn't exist |
| 409 | Conflict | Duplicate resource (email already exists) |
| 500 | Internal Server Error | Unexpected server error (bug) |
Rules for clean REST URLs
| โ Bad (don't do) | โ Good (do this) |
|---|---|
| /getStudents | /students |
| /createStudent | POST /students |
| /deleteStudent/1 | DELETE /students/1 |
| /students/getEnrolledCourses/1 | /students/1/courses |
| /Students (uppercase) | /students (lowercase) |
| /student_data (underscores) | /students (hyphens if needed) |
new ResourceNotFoundException("Student not found with id: " + id) and Spring will automatically return a 404 JSON response.Postman is the standard tool for testing REST APIs. Download it from postman.com.
GET all students
Method: GET ยท URL: http://localhost:8080/api/students ยท Click Send
POST create student
Method: POST ยท URL: http://localhost:8080/api/students
Headers: Content-Type: application/json
Body (raw JSON):
GET student by id
Method: GET ยท URL: http://localhost:8080/api/students/1
DELETE student
Method: DELETE ยท URL: http://localhost:8080/api/students/1 ยท Expect 204 No Content
REST (Representational State Transfer) is an architectural style for distributed hypermedia systems. Key principles: Stateless (no session state on server), Client-Server (separation of concerns), Uniform Interface (HTTP methods + status codes), Cacheable (responses can be cached), Layered System (client doesn't know if it's talking to the actual server or a proxy).
PUT replaces the entire resource with the request body. If you omit a field, it's set to null/default. Idempotent.
PATCH applies a partial update โ only the fields provided in the request body are changed. Other fields remain untouched. Used when you want to update just one field (e.g., just the email) without sending the entire object.
2xx Success: 200 OK (request succeeded), 201 Created (resource created), 204 No Content (success, nothing to return โ DELETE).
4xx Client Error: 400 Bad Request (invalid input), 401 Unauthorized (not authenticated), 403 Forbidden (not authorised), 404 Not Found (resource doesn't exist), 409 Conflict (duplicate).
5xx Server Error: 500 Internal Server Error (unhandled exception), 503 Service Unavailable (server overloaded).
ResponseEntity represents the entire HTTP response including status code, headers, and body. It gives you full control over the response.
Instead of just returning an object (which always gives 200), you can return ResponseEntity.status(201).body(savedStudent) or ResponseEntity.notFound().build().
@RestControllerAdvice is used to define a global exception handler that applies to all controllers. You define @ExceptionHandler methods that intercept specific exceptions and return appropriate HTTP responses.
Without it, unhandled exceptions return a generic 500 error with a Spring whitepage. With it, you return clean JSON error responses with correct status codes.
@PathVariable extracts a value embedded in the URL path: /students/{id} โ the id is part of the path structure.
@RequestParam extracts a query parameter from the URL: /students?city=Pune โ city is a query param after the ?
Use PathVariable for identifying a specific resource. Use RequestParam for filtering, sorting, or pagination.