This is a simple REST API that allows users to create and manage accounts, and transfer money between them. The API is built using Java 17 and Spring Boot 3.0.4, and uses a PostgreSQL database to store account information and transaction history. It is designed to be stateless and uses JWT for authentication and authorization.
The API includes the following endpoints:
POST /api/v1/accounts
: Creates a new account with a starting balance.
GET /api/v1/accounts/{id}
: Retrieves the account information.
POST /api/v1/transfers
: Transfers money from one account to another.
POST /api/v1/auth/register
: Generates a JWT access token for a user.
POST /api/v1/auth/authenticate
: Authenticates user and responds with a JWT access token.
The API enforces the following constraints:
- An account cannot have a negative balance.
- The transfer amount must be greater than zero.
- If the source account does not have enough funds, the transfer will fail.
- The API is designed to handle concurrent requests and avoid race conditions.
To build and run the API locally, you'll need the following dependencies:
Java 17
PostgreSQL
Docker (optional)
Before running the API, you'll need to create a new PostgreSQL database:
CREATE DATABASE bank-api;
To run the API, you can either run it directly from your IDE or build a Docker image and run it inside a container.
Clone the repository.
Open the project in your favorite IDE.
Rename application.properties.example
to application.properties
.
Configure the database connection in application.properties
to match yor db and db credentials.
Run the MoneyTransferApplication class.
Clone the repository.
Build the Docker image:
mvn install
docker build -t money-transfer-0.0.1.jar .
Run the Docker container:
docker run -p 8080:8080 money-transfer-0.0.1.jar
The API is stateless and uses JWT for authentication and authorization.
To authenticate, clients must send a POST request to /api/v1/auth/authenticate
with a valid username and password.
POST /api/v1/auth/authenticate
Content-Type: application/json
{
"username": "your-username",
"password": "your-password"
}
The API will respond with a JWT access token, which clients must include in the Authorization header of all subsequent requests.
HTTP/1.1 200 OK
Content-Type: application/json
{
"token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9..."
}
By default, the API is configured to use the HS256 algorithm to sign and verify JWT tokens.
The API also includes Spring Boot Actuator for health checks and monitoring. The following endpoints are available:
/actuator/health
: Returns the health status of the application.
/actuator/info
: Returns custom application information (configured in application.properties).
By default, these endpoints are only available to authenticated clients.
To run the tests, you can use the following command:
mvn test
This project is licensed under the MIT License. See the LICENSE file for details.