This is a comprehensive REST API built with Spring Boot to simulate the full lifecycle of a loan, from origination and payment tracking to closure and default. It's designed with a clean, layered architecture (Controller-Service-Repository) and follows best practices for API design and data handling.
- Loan Lifecycle Management: Create, track, and close loans.
- Payment Tracking: Record individual payments against a loan.
- Automated Business Rules:
- EMI Calculation: Automatically calculates the Simple Interest, Total Repayable Amount, and Monthly EMI when a loan is created.
- Overpayment Prevention: The API rejects any payment that would exceed the loan's remaining balance.
- Auto-Closure: Automatically transitions a loan's status from
ACTIVEtoCLOSEDwhen the final payment is made. - Auto-Default: A daily scheduled job checks for overdue payments. If a loan is past its grace period (10 days) and is underpaid, its status is automatically set to
DEFAULTED.
- Pagination & Filtering: The
GET /loansendpoint supports full pagination and sorting, as well as filtering byLoanStatus. - Error Handling: Provides clear, JSON-based error messages for common issues (e.g., resource not found, invalid payments, validation errors).
- Framework: Spring Boot 3+
- Language: Java 17+
- Data: Spring Data JPA (Hibernate)
- API: Spring Web (REST Controllers)
- Database: MySQL
- Scheduling: Spring Task (
@Scheduled) - Utilities: Lombok
- Build: Apache Maven
- Java 17 or higher
- Apache Maven 3.6+
- A running MySQL server
-
Create a new database in MySQL:
CREATE DATABASE loan_db;
-
Update the
src/main/resources/application.propertiesfile with your MySQL username and password:# MySQL Database Connection spring.datasource.url=jdbc:mysql://localhost:3306/loan_db?createDatabaseIfNotExist=true spring.datasource.username=root spring.datasource.password=your_mysql_password spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver # JPA Configuration spring.jpa.hibernate.ddl-auto=update spring.jpa.show-sql=true spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQLDialect
You can run the application using the Spring Boot Maven plugin:
mvn spring-boot:run