A Spring Boot REST API for Eagle Bank with Keycloak integration, following Test-Driven Development (TDD) best practices.
- Spring Boot 3.2.0 with Java 21
- Keycloak OAuth2 integration for authentication and authorization
- PostgreSQL database with H2 for testing
- Swagger/OpenAPI UI for interactive API documentation and testing
- Test-Driven Development (TDD) approach
- Dev Container support for consistent development environment
- Docker Compose for local development
- Comprehensive testing with unit, integration, and end-to-end tests
- OpenAPI 3.1 specification compliance
eagle-bank-api-java/
βββ .devcontainer/ # Dev container configuration
βββ src/
β βββ main/
β β βββ java/com/eaglebank/
β β β βββ config/ # Security and application configuration
β β β βββ controller/ # REST controllers
β β β βββ domain/ # Domain models and entities
β β β βββ dto/ # Data Transfer Objects
β β β βββ exception/ # Global exception handling
β β β βββ repository/ # Data access layer
β β β βββ service/ # Business logic layer
β β βββ resources/
β β βββ db/ # Database initialization scripts
β β βββ application.yml # Application configuration
β βββ test/
β βββ java/com/eaglebank/
β β βββ controller/ # Controller tests
β β βββ domain/ # Domain model tests
β β βββ integration/ # Integration tests
β β βββ service/ # Service layer tests
β βββ resources/ # Test configuration
βββ docs/ # OpenAPI specification
βββ docker-compose.yml # Local development environment
βββ pom.xml # Maven dependencies
βββ README.md # This file
- Docker and Docker Compose
- Java 21
- Maven 3.8+
- VS Code with Dev Containers extension (recommended)
-
Clone the repository Example git repository URL - https://github.com/harish516/eagle-bank-api-java.git
git clone <repository-url> cd eagle-bank-api-java
-
Open in VS Code with Dev Containers
- Install the "Dev Containers" extension in VS Code
- Open the project folder
- When prompted, click "Reopen in Container"
-
Start the development environment
sudo docker-compose up -d
-
Add the dev container to the same docker network where postgres and keycloak are running Example - sudo docker network connect eagle-bank-api-java_eagle-bank-network clever_albattani
sudo docker network connect <docker-network> <vs-code-dev-container-name>
-
Run the application
mvn spring-boot:run -Dspring-boot.run.profiles=dev
- Spring Boot Application: http://localhost:8080
- Keycloak Admin Console: http://localhost:8180
- Username:
admin - Password:
admin
- Username:
- PostgreSQL: localhost:5432
- Database:
eagle_bank - Username:
eagle_user - Password:
eagle_password
- Database:
-
Access Keycloak Admin Console
- URL: http://localhost:8180
- Login with admin/admin
-
Create Realm
- Click "Create Realm"
- Name:
eagle-bank
-
Create Client
- Go to "Clients" β "Create"
- Client ID:
eagle-bank-api - Client Protocol:
openid-connect - Access Type:
public
-
Configure Client
- Valid Redirect URIs:
http://localhost:8080/* - Web Origins:
http://localhost:8080 - Save
- Valid Redirect URIs:
-
Create User
- Go to "Users" β "Add User"
- Username:
testuser - Email:
test@example.com - Set password in "Credentials" tab
To get an authentication token using Postman:
The host should be eagle-bank-keycloak to match with valid iss claim. So, map the host eagle-bank-keycloak to 127.0.0.1 in hosts file.
- Method: POST
- URL:
http://eagle-bank-keycloak:8180/realms/eagle-bank/protocol/openid-connect/token - Headers:
Content-Type: application/x-www-form-urlencoded - Body (form-data):
grant_type:passwordclient_id:eagle-bank-apiusername:testuserpassword:Test1234
Example cURL command:
curl -X POST "http://eagle-bank-keycloak:8180/realms/eagle-bank/protocol/openid-connect/token" \
-H "Content-Type: application/x-www-form-urlencoded" \
-d "grant_type=password&client_id=eagle-bank-api&username=testuser&password=Test1234"Postman Collection Link for Postman collection is https://www.postman.com/speeding-equinox-655623/workspace/eagle-bank/collection/865674-2030b50a-9f0a-4936-bded-073ac52ebde4
Postman collection is also committed to git repository under docs/postman
The Eagle Bank API includes Swagger UI for interactive API documentation and testing:
- Swagger UI: http://localhost:8080/eagle-bank/swagger-ui.html
- OpenAPI JSON: http://localhost:8080/eagle-bank/api-docs
Features:
- π Browse all API endpoints with detailed documentation
- π§ͺ Test APIs directly from the browser (no Postman needed!)
- οΏ½ OAuth2 Login - authenticate with username/password (no manual JWT tokens!)
- π JWT Bearer Token support for advanced users
- π Request/Response examples with validation
- π Data models with schema definitions
Quick Start with Swagger UI:
- Start the application:
./mvnw spring-boot:run - Open http://localhost:8080/eagle-bank/swagger-ui.html in your browser
- Create a test user in Keycloak (see detailed guide below)
- Use
POST /api/v1/usersto create a test user (no auth required) - Use
GET /api/v1/usersto view all the users (no auth required - it provided for testing purpose) - Click "Authorize" in Swagger UI β "oauth2" β enter username/password
- Test protected endpoints like
GET /api/v1/users/me
π Detailed Guide: docs/SWAGGER_GUIDE.md
POST /api/v1/users- Create a new userGET /api/v1/users/{userId}- Get user by IDPATCH /api/v1/users/{userId}- Update userDELETE /api/v1/users/{userId}- Delete user
POST /api/v1/accounts- Create a new bank accountGET /api/v1/accounts- List all accountsGET /api/v1/accounts/{accountNumber}- Get account by numberPATCH /api/v1/accounts/{accountNumber}- Update accountDELETE /api/v1/accounts/{accountNumber}- Delete account
POST /api/v1/accounts/{accountNumber}/transactions- Create transactionGET /api/v1/accounts/{accountNumber}/transactions- List transactionsGET /api/v1/accounts/{accountNumber}/transactions/{transactionId}- Get transaction
mvn testmvn test -Dtest=*Testmvn test -Dtest=*IntegrationTestThis project follows Test-Driven Development principles:
- Write a failing test for the feature you want to implement
- Write the minimum code to make the test pass
- Refactor the code to improve design while keeping tests green
- Repeat for the next feature
- Unit Tests: Test individual components in isolation
- Integration Tests: Test component interactions
- End-to-End Tests: Test complete user workflows
CREATE TABLE users (
id VARCHAR(255) PRIMARY KEY,
name VARCHAR(255) NOT NULL,
address_line1 VARCHAR(255) NOT NULL,
address_line2 VARCHAR(255),
address_line3 VARCHAR(255),
address_town VARCHAR(255) NOT NULL,
address_county VARCHAR(255) NOT NULL,
address_postcode VARCHAR(255) NOT NULL,
phone_number VARCHAR(255) NOT NULL,
email VARCHAR(255) NOT NULL UNIQUE,
created_timestamp TIMESTAMP NOT NULL,
updated_timestamp TIMESTAMP NOT NULL
);CREATE TABLE bank_accounts (
account_number VARCHAR(255) PRIMARY KEY,
sort_code VARCHAR(255) NOT NULL,
name VARCHAR(255) NOT NULL,
account_type VARCHAR(255) NOT NULL,
balance DECIMAL(10,2) NOT NULL,
currency VARCHAR(255) NOT NULL,
user_id VARCHAR(255) NOT NULL,
created_timestamp TIMESTAMP NOT NULL,
updated_timestamp TIMESTAMP NOT NULL,
FOREIGN KEY (user_id) REFERENCES users(id)
);CREATE TABLE transactions (
id VARCHAR(255) PRIMARY KEY,
amount DECIMAL(10,2) NOT NULL,
currency VARCHAR(255) NOT NULL,
type VARCHAR(255) NOT NULL,
reference VARCHAR(255),
bank_account_id VARCHAR(255) NOT NULL,
user_id VARCHAR(255) NOT NULL,
created_timestamp TIMESTAMP NOT NULL,
FOREIGN KEY (bank_account_id) REFERENCES bank_accounts(account_number)
);The application uses Keycloak for OAuth2 authentication:
- JWT Token Validation: Validates tokens from Keycloak
- Role-Based Access Control: Uses Keycloak roles for authorization
- Stateless Authentication: No server-side session storage
- CORS: Configured for local development
- CSRF: Disabled for API endpoints
- Session Management: Stateless (JWT-based)
/actuator/health- Application health status/actuator/info- Application information/actuator/metrics- Application metrics/actuator/prometheus- Prometheus metrics
If your project does not already include the Maven Wrapper (mvnw and .mvn/), generate it with:
mvn -N io.takari:maven:wrapper
### Docker Build
```bash
mvn clean package
docker build -t eagle-bank-api .docker run -p 8080:8080 eagle-bank-api- Follow TDD: Write tests first
- Use meaningful commit messages
- Keep functions small and focused
- Add comprehensive documentation
- Ensure all tests pass before committing
-
Keycloak Connection Issues
- Ensure Keycloak is running:
docker-compose ps - Check Keycloak logs:
docker-compose logs keycloak
- Ensure Keycloak is running:
-
Database Connection Issues
- Ensure PostgreSQL is running:
docker-compose ps - Check database logs:
docker-compose logs postgres
- Ensure PostgreSQL is running:
-
Port Conflicts
- Check if ports 8080, 8180, 5432 are available
- Stop conflicting services or change ports in docker-compose.yml
- Application logs: Check console output or logs directory
- Docker logs:
docker-compose logs <service-name> - Database logs:
docker-compose logs postgres
This project is licensed under the MIT License.