I'm sharing a quick guide on building RESTful APIs using
Spring Boot without a database.
We'll use an in-memory list to simulate the data storage!
Let's dive into setting up the Controller, Service, and "DAO" layers. 57
Step 1: Define the Model @
First, we create a simple Product entity:
public class Product {
private Long id;
private String name;
private double price;
// Getters and setters
Step 2: Simulate the DAO Layer (In-Memory List) [7
Here, we use a List
to simulate a database and add a few initial products.
@Repository
public class ProductRepository {
private List productList = new ArrayList<>();
private Long idCounter = 1L;
public ProductRepository() {
// Adding some initial products to the listproductList.add(new Product(idCounter++, "Laptop", 1200.00));
productList.add(new Product(idCounter++, "Smartphone",
800.00));
productList.add(new Product(idCounter++, "Headphones",
15@.00));
}
public Product save(Product product) {
product. setId(idCounter++);
productList.add(product);
return product;
public List findAll() {
return productList;
public Optional findById(Long id) {
return productList.stream().filter(product ->
product. getId().equals(id)).findFirst() ;
}
public void deleteById(Long id) {
productList.removelf(product -> product.getId().equals(id));
Step 3: Service Layer
The service layer interacts with our in-memory repository.@service
public class ProductService {
@autowired
private ProductRepository repository;
public Product createProduct(Product product) {
return repository. save(product) ;
public List getAllProducts() {
return repository. findAll();
public Product getProductById(Long id) {
return repository. findById(id).orElseThrow(() -> new
RuntimeException("Product not found"));
}
public Product updateProduct(Long id, Product newProduct) {
Product product = getProductById(id);
product. setName(newProduct.getName());
product. setPrice(newProduct.getPrice()) 5
return product;
public void deleteProduct(Long id) {
repository. deleteById(id) ;Step 4: Controller Layer £7
Finally, the controller exposes the API endpoints
Create (POST) 4
@RestController
@RequestMapping("/api/products")
public class ProductController {
@autowired
private ProductService service;
@PostMapping
public Product createProduct(@RequestBody Product product) {
return service.createProduct (product);
@GetMapping
public List getAllProducts() {
return service.getAllProducts();
@GetMapping("/{id}")
public Product getProductById(@PathVariable Long id) {
return service.getProductById(id) ;
@PutMapping("/{id}")
public Product updateProduct(@PathVariable Long id, @RequestBody
Product newProduct) {return service.updateProduct(id, newProduct);
@DeleteMapping("/{id}")
public void deleteProduct(@PathVariable Long id) {
service. deleteProduct (id) ;
Step 5: Run the Application &»
You can now start your Spring Boot application, and the API is ready to go with an in-
memory list! Here's what happens:
© By default, the list has 3 products: a Laptop, Smartphone, and Headphones.
* You can test the API at http: //localhost:808@/api/products to perform CRUD
operations.