🎓 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 REST APIs for a simple Banking application using Spring Boot, Spring Data JPA (Hibernate) and MySQL database.
We'll create a simple version of Banking App with basic functionalities: creating a bank account, fetching account details, and making a deposit/withdrawal.
1. Set up a Spring Boot project
Let's launch Spring Initializr and fill up the following project details:
Project: Maven Project (or Gradle)
Language: Java
Packaging: Jar
Java version: 17
Dependencies: Spring Web, Spring Data JPA, MySQL Driver, and Lombok
Download, extract the project and import in your favorite IDE.
2. Configure the MySQL database
Let's open src/main/resources/application.properties file and add the MySQL configuration properties:
spring.datasource.url=jdbc:mysql://localhost:3306/banking_app
spring.datasource.username=root
spring.datasource.password=Mysql@123
spring.jpa.hibernate.ddl-auto=updateMake sure that you change the MySQL database username and password as per your MySQL installation on your machine.
The spring.jpa.hibernate.ddl-auto=update line ensures that tables and columns get automatically created or updated based on your JPA entities.
3. Create the Account entity
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import lombok.Getter;
import lombok.Setter;
@Getter
@Setter
@Entity
public class Account {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String accountHolderName;
private double balance;
}Note that we are using Lombok annotations to reduce the boilerplate code.
Account Entity represents a bank account with properties like ID, account holder name, and balance. It uses JPA annotations to define the entity and its mapping to the database.
4. Create the JPA repository for the Account entity
Let's create AccountRepository interface that extends JpaRepository. Once AccountRepository interface extends JpaRepository then it will get the CRUD methods to perform CRUD database operations on Account entity.
import org.springframework.data.jpa.repository.JpaRepository;
public interface AccountRepository extends JpaRepository<Account, Long> {
}5. Create the service class
The AccountService class contains the core business logic for the application:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Optional;
@Service
public class AccountService {
@Autowired
private AccountRepository accountRepository;
public Account createAccount(Account account) {
return accountRepository.save(account);
}
public Optional<Account> getAccount(Long id) {
return accountRepository.findById(id);
}
public Account deposit(Long id, double amount) {
Account account = getAccount(id).orElseThrow(() -> new RuntimeException("Account not found"));
account.setBalance(account.getBalance() + amount);
return accountRepository.save(account);
}
public Account withdraw(Long id, double amount) {
Account account = getAccount(id).orElseThrow(() -> new RuntimeException("Account not found"));
if (account.getBalance() < amount) {
throw new RuntimeException("Insufficient funds");
}
account.setBalance(account.getBalance() - amount);
return accountRepository.save(account);
}
}createAccount: Adds a new account to the database.
getAccount: Retrieves an account's details by ID.
deposit: Deposits a specified amount into an account.
withdraw: Withdraws a specified amount from an account.
6. Create the REST controller
The AccountController class class exposes the business logic as RESTful API endpoints.
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.Map;
@RestController
@RequestMapping("/api/accounts")
public class AccountController {
@Autowired
private AccountService accountService;
@PostMapping
public Account createAccount(@RequestBody Account account) {
return accountService.createAccount(account);
}
@GetMapping("/{id}")
public Account getAccount(@PathVariable Long id) {
return accountService.getAccount(id).orElseThrow(() -> new RuntimeException("Account not found"));
}
@PostMapping("/{id}/deposit")
public Account deposit(@PathVariable Long id, @RequestBody Map<String, Double> request) {
Double amount = request.get("amount");
return accountService.deposit(id, amount);
}
@PostMapping("/{id}/withdraw")
public Account withdraw(@PathVariable Long id, @RequestBody Map<String, Double> request) {
Double amount = request.get("amount");
return accountService.withdraw(id, amount);
}
}Run and Test Application
Navigate to the main application class (with @SpringBootApplication annotation) and run it as a Java application.
1. Create a new bank account:
HTTP Method: POST
URL: http://localhost:8080/api/accounts
Sample Request Body:
{
"accountHolderName": "Ramesh",
"balance": 1000.00
}2. Fetch the details of a specific account:
HTTP Method: GET
URL: http://localhost:8080/api/accounts/{id} (Replace {id} with the actual ID of the account you want to fetch.)
3. Deposit money into an account:
{
"amount": 200.00
}4. Withdraw money from an account:
HTTP Method: POST
URL: http://localhost:8080/api/accounts/{id}/withdraw (Replace {id} with the actual ID of the account you want to withdraw money from.)
Sample Request Body:
{
"amount": 150.00
}Conclusion
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




Comments
Post a Comment
Leave Comment