A Generic Spring Boot voting application with blockchain-inspired immutable ledger, hexagonal architecture, and AI capabilities.
π§βπ³ Features
- Hexagonal Architecture: Clean separation of concerns with domain, application, infrastructure, and API layers
- Capable of integrating to different programming languages and ui frameworks
- πEmail-based Registration & Authentication: Simple JWT-based authentication system
- πCRUD Voting System: Create, read, and participate in voting polls
- πBlockchain-Inspired Ledger: Immutable vote records using SHA-256 hash chaining
- πAI Integration:
- OpenAI GPT for vote description enhancement and insights
- xAI Grok for advanced vote analysis
- π RESTful API: Designed for React Native/React app consumption
- ποΈAPI Documentation: Interactive Swagger/OpenAPI documentation
- ποΈTamper-Proof Verification: Blockchain integrity verification endpoint
π§ What Youβll Learn
- Implement authentication & email verification
- Build a springboot backend services(CRUD style)
- Integration of AI and blockchain technologies
- Framework: Spring Boot 3.2.0 with AI
- Language: Java 19 / Java 21 (Docker)
- Database: H2 (in-memory for development)
- Security: Spring Security with JWT
- AI Services: OpenAI API, xAI API
- Documentation: SpringDoc OpenAPI 3
- Build Tool: Maven
- Testing: JUnit 5, Mockito, Spring Boot Test
src/main/java/com/voting/
βββ domain/ # Core business logic
β βββ model/ # Entities (User, Vote, VoteOption, BlockchainRecord)
β βββ port/ # Interfaces (Repositories, Services)
β βββ valueobject/ # Value objects (VoteRecord)
βββ application/ # Use cases
β βββ usecase/ # Business workflows
βββ infrastructure/ # External adapters
β βββ persistence/ # JPA repositories
β βββ ai/ # AI service implementations
β βββ blockchain/ # Blockchain service
β βββ security/ # JWT and authentication
βββ api/ # HTTP interface
βββ controller/ # REST controllers
βββ dto/ # Data transfer objects
βββ config/ # Configuration classes
- Java 19 or higher (Java 21 recommended for Docker deployments)
- Maven 3.6+
- (Optional) OpenAI API key for AI features
- (Optional) xAI API key for Grok integration
Note: The project is configured to use Java 19, but the Docker image uses Java 21 for containerized deployments. Both versions are fully supported.
Create a .env file or set these environment variables:
SESSION_SECRET=your-super-secret-jwt-key-min-32-characters
OPENAI_API_KEY=your-openai-api-key (optional)
XAI_API_KEY=your-xai-api-key (optional)# Install dependencies and build
mvn clean install
# Run the application
mvn spring-boot:runThe application will start on http://localhost:8085
# Build the Docker image
docker build -t blockchain-voting:latest .
# Run the container
docker run -p 8085:8085 \
-e SESSION_SECRET=your-secret-key \
-e OPENAI_API_KEY=your-openai-key \
-e XAI_API_KEY=your-xai-key \
blockchain-voting:latestOnce the application is running, access the interactive API documentation:
- Swagger UI: http://localhost:8085/swagger-ui.html
- OpenAPI Spec: http://localhost:8085/v3/api-docs
POST /api/auth/register- Register new userPOST /api/auth/login- Login and get JWT token
POST /api/votes- Create a new voteGET /api/votes- Get all votesGET /api/votes/active- Get active votesGET /api/votes/{id}- Get specific votePOST /api/votes/{id}/cast- Cast a voteGET /api/votes/{id}/history- Get blockchain history for a vote
GET /api/ai/insights/{voteId}- Get AI-powered vote insights
GET /api/blockchain/verify- Verify blockchain integrity
The application uses a blockchain-inspired immutable ledger:
- Each vote creates a block with SHA-256 hash
- Blocks are chained using previous block's hash
- Genesis block uses a predefined hash
- Vote tampering can be detected via
/api/blockchain/verify
{
"blockNumber": 0,
"previousHash": "0000...0000",
"currentHash": "a1b2c3...",
"timestamp": "2024-01-01T12:00:00",
"data": "User:1 voted for option:2 in vote:1",
"nonce": "1234567890"
}The project includes comprehensive unit and integration tests covering all major components:
# Run all tests
mvn test
# Run specific test class
mvn test -Dtest=VoteControllerIntegrationTest
# Run tests in continuous mode
mvn test -Dmaven.test.failure.ignore=trueTest Coverage:
- Unit Tests: Domain entities, use cases, and blockchain service
- Integration Tests: REST API endpoints with authentication
Tests can use JUnit 5, Mockito for mocking, and Spring Boot Test for integration testing.
Access the H2 database console at: http://localhost:8085/h2-console
- JDBC URL:
jdbc:h2:mem:votingdb - Username:
sa - Password: (empty)
Use the Swagger UI or tools like Postman/cURL:
# Register a user
curl -X POST http://localhost:8085/api/auth/register \
-H "Content-Type: application/json" \
-d '{"email":"user@example.com","password":"password123","name":"John Doe"}'
# Login
curl -X POST http://localhost:8085/api/auth/login \
-H "Content-Type: application/json" \
-d '{"email":"user@example.com","password":"password123"}'
# Create a vote (with JWT token)
curl -X POST http://localhost:8085/api/votes \
-H "Content-Type: application/json" \
-H "Authorization: Bearer YOUR_JWT_TOKEN" \
-d '{
"title":"Favorite Programming Language",
"description":"Vote for your favorite programming language",
"options":["Java"],
"startDate":"2024-01-01T00:00:00",
"endDate":"2026-12-31T23:59:59",
"useAIEnhancement":false
}'- Enhances vote descriptions for clarity and engagement
- Provides insights on voting patterns
- Requires
OPENAI_API_KEYenvironment variable
- Alternative AI provider for vote analysis
- Requires
XAI_API_KEYenvironment variable
The application includes a multi-stage Dockerfile optimized for production:
Build stage: Compiles the application with Maven Runtime stage: Runs the application with minimal JRE
For production deployments, consider:
- Using PostgreSQL instead of H2
- Enabling HTTPS/TLS
- Implementing rate limiting
- Setting up monitoring and logging
No Copyright No Rights Reserved No Attribution Required For more information, see the MIT license.