Learning Goal:
โข Learn the principles and practice of designing RESTful APIs.
Detailed Content:
โข REST architecture basics: statelessness, resources, URI design
โข HTTP methods (GET, POST, PUT, DELETE) and status codes
โข Good vs bad REST API examples
โข Designing CRUD APIs for simple modules
โข Swagger Editor: write a basic API spec draft
Modern web services often rely on RESTโRepresentational State Transferโas the architectural style for building clean, predictable APIs. At its core, REST is about treating data as resources and interacting with these resources using standard HTTP methods.
A RESTful API must be stateless, meaning each HTTP request from a client must contain all the information the server needs to process it. The server does not store session state between requests. This makes REST simple, scalable, and easy to cache.
Good URI design is another hallmark of a RESTful API. URIs should clearly represent resources, not actions. For example, you should design endpoints like /api/users/
to represent a collection of user resources, and /api/users/123
to represent a single user with ID 123
. The URI should not contain verbs like /api/getUser
or /api/updateUser
.
In REST, the standard HTTP methods map naturally to CRUD operations:
GET retrieves a resource or collection.
POST creates a new resource.
PUT updates a whole resource.
DELETE removes a resource.
Status codes communicate what happened. For example:
200 OK
means success.
201 Created
means a new resource was successfully created.
204 No Content
means the request succeeded but there is no response body.
400 Bad Request
means the client sent something invalid.
404 Not Found
means the resource does not exist.
500 Internal Server Error
means something went wrong on the server.
A bad API design might look like this:
These endpoints break REST principles because they put actions in the URI and misuse HTTP verbs.
A good RESTful design for the same functionality would look like this:
Here, the URI represents resources (users
), and HTTP methods express the action.
Letโs see how you might implement a simple User CRUD API in Java using Spring Boot.
User Entity:
Repository:
Controller:
This simple controller demonstrates proper RESTful design:
URIs describe the resource: /api/users/{id}
HTTP methods do the work: POST
creates, GET
reads, PUT
updates, DELETE
removes.
Status codes correctly communicate results (201 Created
, 200 OK
, 404 Not Found
, 204 No Content
).
To make your API clear to other developers, always document it properly. Swagger (now known as OpenAPI) is a popular way to define an API spec in a human- and machine-readable format.
Hereโs a simple example of how your User API might look in Swagger YAML:
You can paste this YAML into Swagger Editor to generate interactive API docs and even auto-generate client/server code stubs.
Designing a RESTful API well is about much more than wiring up routes. Itโs about clear resource-oriented URIs, consistent HTTP methods, correct status codes, and good documentation. Combine this with clean, modular Java code and you have an API thatโs easy to use, maintain, and evolve.