🏦

Banking Application with Spring Boot

Jun 22, 2024

Banking Application with Spring Boot, Spring Data JPA, and MySQL

Overview

  • Build a simple banking application.
  • Use the latest versions of Spring Boot, Spring Data JPA, and MySQL.

Setup

  1. Start Spring Initializer: Go to start.spring.io.
    • Project: Maven
    • Language: Java
    • Spring Boot Version: 3.2.1 (latest stable)
  2. Project Metadata:
    • Group: net.javaguides
    • Artifact: banking-app
    • Description: Demo project for Spring Boot banking app
    • Package Name: net.java.banking
    • Packaging: jar
    • Java Version: 17
  3. Dependencies:
    • Spring Web: For building REST APIs.
    • Spring Data JPA: For JPA repositories and hibernate.
    • MySQL Driver: For connecting Spring Boot with MySQL.
    • Lombok: To reduce boilerplate code.
  4. Generate: Download the .zip file and unzip it.
  5. Open in IDE: Import the project in IntelliJ IDEA.

Configuration

  1. MySQL Database:
    • Create the database using MySQL Workbench.
    • SQL Statement: CREATE DATABASE banking_app;
  2. Spring Boot Application:
    • Go to src/main/resources/application.properties.
    • Configure MySQL Database details: spring.datasource.url=jdbc:mysql://localhost:3306/banking_app spring.datasource.username=root spring.datasource.password=password spring.jpa.hibernate.ddl-auto=update
  3. Run Application: Ensure connectivity with database.

Project Structure

  • Packages:
    • entity: For JPA entities
    • controller: For REST controllers
    • service: For service layer
    • repository: For Spring Data JPA repositories
    • dto: For data transfer objects
    • mapper: For converting DTOs to entities and vice versa

Implementation

  1. Entity: Account
    • Fields: id, accountHolderName, balance
    • Annotations: @Entity, @Table(name="accounts"), @Id, @GeneratedValue(strategy = GenerationType.IDENTITY), @Column(name="account_holder_name")
  2. DTO: AccountDto
    • Fields: id, accountHolderName, balance
    • Lombok Annotations: @Data, @AllArgsConstructor, @NoArgsConstructor
  3. Mapper: AccountMapper
    • Methods: mapToAccount(AccountDto accountDto), mapToAccountDto(Account account)
  4. Repository: AccountRepository
    • Interface extending JpaRepository<Account, Long>
  5. Service: AccountService
    • Interface: createAccount(AccountDto accountDto), getAccountById(Long id), getAllAccounts(), deposit(Long id, Double amount), withdraw(Long id, Double amount), deleteAccount(Long id)
    • Implementation: AccountServiceImpl
  6. Controller: AccountController
    • REST Endpoints:
      • createAccount(AccountDto accountDto)
      • getAccountById(@PathVariable Long id)
      • getAllAccounts()
      • deposit(@PathVariable Long id, @RequestBody Map<String, Double> request)
      • withdraw(@PathVariable Long id, @RequestBody Map<String, Double> request)
      • deleteAccount(@PathVariable Long id)

REST API Testing

  1. Add Account:
    • URL: http://localhost:8080/api/accounts
    • Method: POST
    • Request Body: { "accountHolderName": "John Doe", "balance": 20000 }
    • Response: { "id": 1, "accountHolderName": "John Doe", "balance": 20000 }
  2. Get Account by ID:
    • URL: http://localhost:8080/api/accounts/{id}
    • Method: GET
    • Response: { "id": 1, "accountHolderName": "John Doe", "balance": 20000 }
  3. Get All Accounts:
    • URL: http://localhost:8080/api/accounts
    • Method: GET
    • Response: [{ "id": 1, "accountHolderName": "John Doe", "balance": 20000 }, { "id": 2, "accountHolderName": "Jane Doe", "balance": 15000 }]
  4. Deposit:
    • URL: http://localhost:8080/api/accounts/{id}/deposit
    • Method: PUT
    • Request Body: { "amount": 5000 }
    • Response: { "id": 1, "accountHolderName": "John Doe", "balance": 25000 }
  5. Withdraw:
    • URL: http://localhost:8080/api/accounts/{id}/withdraw
    • Method: PUT
    • Request Body: { "amount": 5000 }
    • Response: { "id": 1, "accountHolderName": "John Doe", "balance": 20000 }
  6. Delete Account:
    • URL: http://localhost:8080/api/accounts/{id}
    • Method: DELETE
    • Response: Account is deleted successfully

Postman Testing

  1. Add Account: Tested using Postman Client.
  2. Get Account by ID: Verified using Postman and database.
  3. Get All Accounts: Verified using Postman and database.
  4. Deposit: Confirmed balance update via Postman and database.
  5. Withdraw: Ensured proper deduction via Postman and database.
  6. Delete Account: Verified account removal using Postman and database.