REST APIs are based on the principles of Representational State Transfer, a software architectural style defined by Roy Fielding in his doctoral dissertation. REST enables stateless communication between clients and servers using HTTP as the transport protocol. Each resource in the system is identified by a unique URL, and clients interact with these resources using standard HTTP methods such as GET, POST, PUT, and DELETE.
The key principles of REST include:
- Statelessness: Each API request from a client to a server must contain all the information necessary to complete the request. The server does not maintain any client state between requests.
- Uniform Interface: REST APIs use standard HTTP methods and status codes to ensure a consistent interface.
- Resource Identification: Each resource is uniquely identified by a URL (e.g.,
/products/1
). - Representation: Resources are represented in formats like JSON, XML, or plain text.
- Client-Server Separation: The client and server are independent; the client only needs to know the resource's URL and does not need to understand server implementation details.
To understand REST APIs better, consider an example of a simple e-commerce system with resources like products
and orders
. Below are some common API endpoints:
HTTP Method | Endpoint | Description |
---|---|---|
GET | /products | Retrieve a list of all products |
POST | /products | Create a new product |
GET | /products/1 | Retrieve details of a product with ID 1 |
PUT | /products/1 | Update the product with ID 1 |
DELETE | /products/1 | Delete the product with ID 1 |
Below is a sample implementation of a REST API for managing products using Spring Boot:
import org.springframework.web.bind.annotation.*; import java.util.ArrayList; import java.util.List; @RestController @RequestMapping("/products") public class ProductController { private List<Product> products = new ArrayList<>(); @GetMapping public List<Product> getAllProducts() { return products; } @PostMapping public Product addProduct(@RequestBody Product product) { products.add(product); return product; } @GetMapping("/{id}") public Product getProductById(@PathVariable int id) { return products.stream() .filter(product -> product.getId() == id) .findFirst() .orElse(null); } @PutMapping("/{id}") public Product updateProduct(@PathVariable int id, @RequestBody Product updatedProduct) { Product product = products.stream() .filter(p -> p.getId() == id) .findFirst() .orElse(null); if (product != null) { product.setName(updatedProduct.getName()); product.setPrice(updatedProduct.getPrice()); } return product; } @DeleteMapping("/{id}") public String deleteProduct(@PathVariable int id) { products.removeIf(product -> product.getId() == id); return "Product deleted successfully"; } }
REST APIs use standard HTTP status codes to indicate the result of an operation. For example:
- 200 OK: The request was successful.
- 201 Created: A new resource was created successfully.
- 400 Bad Request: The request was invalid.
- 404 Not Found: The requested resource could not be found.
- 500 Internal Server Error: A server-side error occurred.
REST APIs play a foundational role in microservices architecture by enabling seamless communication between services. They provide a simple and standardized way to expose resources and allow systems to evolve independently. By following REST principles and best practices, you can build scalable, maintainable, and interoperable APIs for your microservices.