🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
In this tutorial, we will learn how to build CRUD REST APIs using Spring boot but without the database.
Basically, we store and retrieve data to and from the in-memory object.
Checkout all Spring boot tutorials at https://www.javaguides.net/p/spring-boot-tutorial.html
Note that we are going to use the latest version of Spring Boot which is Spring Boot 3.
1. Creating a Spring Boot Application
>> Create Spring Boot Project in Spring Tool Suite [STS]
2. Create Project Packaging Structure
3. Maven Dependencies
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.0.0-SNAPSHOT</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>net.javaguides</groupId>
<artifactId>springboot-crud-example-without-database</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springboot-crud-example-without-database</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
<repository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url>
<releases>
<enabled>false</enabled>
</releases>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</pluginRepository>
<pluginRepository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url>
<releases>
<enabled>false</enabled>
</releases>
</pluginRepository>
</pluginRepositories>
</project>4. Create Model - Product.java
package net.javaguides.springboot.model;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Product {
private int id;
private String name;
private int quantity;
private double price;
}5. Create Repository - ProductRepository
package net.javaguides.springboot.repository;
import net.javaguides.springboot.model.Product;
import org.springframework.stereotype.Repository;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
@Repository
public class ProductRepository {
private List<Product> list = new ArrayList<Product>();
public void createProducts() {
list = List.of(
new Product(1, "product 1", 10, 1000),
new Product(2, "product 2", 20, 2000),
new Product(3, "product 3", 30, 3000)
);
}
public List<Product> getAllProducts() {
return list;
}
public Product findById(int id){
for (int i = 0; i < list.size(); i++) {
if (list.get(i).getId() == (id)) {
return list.get(i);
}
}
return null;
}
public List<Product> search(String name) {
return list.stream().filter(x -> x.getName().startsWith(name)).collect(Collectors.toList());
}
public Product save(Product p) {
Product product = new Product();
product.setId(p.getId());
product.setName(p.getName());
product.setQuantity(p.getQuantity());
product.setPrice(p.getPrice());
list.add(product);
return product;
}
public String delete(Integer id) {
list.removeIf(x -> x.getId() == (id));
return null;
}
public Product update(Product product) {
int idx = 0;
int id = 0;
for (int i = 0; i < list.size(); i++) {
if (list.get(i).getId() == (product.getId())) {
id = product.getId();
idx = i;
break;
}
}
Product product1 = new Product();
product1.setId(id);
product1.setName(product.getName());
product1.setQuantity(product.getQuantity());
product1.setPrice(product.getPrice());
list.set(idx, product);
return product1;
}
}6. Create Service - ProductService.java
package net.javaguides.springboot.service;
import net.javaguides.springboot.model.Product;
import net.javaguides.springboot.repository.ProductRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class ProductService {
@Autowired
private ProductRepository repository;
public Product saveProduct(Product product) {
return repository.save(product);
}
public List<Product> getProducts() {
return repository.getAllProducts();
}
public Product getProductById(int id) {
return repository.findById(id);
}
public String deleteProduct(int id) {
repository.delete(id);
return "product removed !! " + id;
}
public Product updateProduct(Product product) {
return repository.update(product);
}
}7. Create Controller - ProductController.java
package net.javaguides.springboot.controller;
import net.javaguides.springboot.model.Product;
import net.javaguides.springboot.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequestMapping("/api/v1/products")
public class ProductController {
@Autowired
private ProductService service;
@PostMapping
public Product addProduct(@RequestBody Product product) {
return service.saveProduct(product);
}
@GetMapping
public List<Product> findAllProducts() {
return service.getProducts();
}
@GetMapping("{id}")
public Product findProductById(@PathVariable int id) {
return service.getProductById(id);
}
@PutMapping
public Product updateProduct(@RequestBody Product product) {
return service.updateProduct(product);
}
@DeleteMapping("{id}")
public String deleteProduct(@PathVariable int id) {
return service.deleteProduct(id);
}
}8. Running the Spring Boot Application
$ mvn spring-boot:run
9. Demo
Create Product REST API:
Get Product By ID REST API:
Get All Products REST API:
Update Product REST API:
Delete Product REST API:
10. Conclusion
In this tutorial, we have seen how to build CRUD REST APIs using Spring boot but without the database. Basically, we have used an in-memory object to store and retrieve the data.
Checkout all Spring boot tutorials at https://www.javaguides.net/p/spring-boot-tutorial.html
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business






Nice
ReplyDelete