A robust and scalable Spring Boot application offering real-time API services with user authentication, post and comment management, and advanced security features. Built with industry best practices, this project is ideal for developers who want a production-ready API backend with a clean and maintainable codebase.
- Features β¨
- Project Structure π
- Getting Started π
- Configuration βοΈ
- API Documentation π
- Testing π§ͺ
- Deployment π
- Contributing π€
- License π
- JWT-based authentication for secure stateless sessions.
- Role-based access control with Admin and User roles.
- Endpoints for user registration and login.
- CRUD operations for blog posts.
- Pagination & sorting for efficient listing.
- Search functionality to find posts by title or description.
- AI-driven content summarization using locally hosted ollama LLM.
- CRUD operations for comments.
- Fetch comments by post ID.
- Ensure comments belong to the correct post.
- Role-based permissions for comment operations.
- CRUD operations for user profiles.
- Role assignment for better access control.
- Retrieve detailed user information.
- JWT token generation and validation.
- Custom user details service for authentication.
- Configurable CORS settings (using environment variables for domain management).
- Rate limiting to prevent abuse of API requests.
- Swagger/OpenAPI integration for interactive API docs.
- Detailed annotations and summaries for each endpoint.
- Externalized configuration using a
.env
file and environment-specific properties. - Easy management of database connections and other settings.
- Custom exception classes for precise error reporting.
- Global exception handler to manage API error responses uniformly.
- JPA repositories for seamless database operations.
- Entity classes with Hibernate ORM for effective object-relational mapping.
- ModelMapper for DTO and entity mapping.
- BCrypt for secure password encoding.
- Postman collections provided for API testing.
- Swagger UI for interactive endpoint testing.
- Docker support with a Dockerfile and docker-compose configuration.
- Maven build configuration for streamlined project management.
- Configurable logging levels using SLF4J with Logback.
- Actuator endpoints for health checks and application metrics.
- Clean, maintainable code following design patterns.
- Well-structured project organization for scalability and readability.
app/
βββ Dockerfile & docker-compose.yml # Docker support for containerized deployment π’
βββ pom.xml # Maven build configuration
βββ src/
β βββ main/
β β βββ java/ # Application source code
β β β βββ config/ # Configuration classes (security, logging, rate limiting, etc.)
β β β βββ controllers/ # REST controllers (API endpoints)
β β β βββ entities/ # JPA entities mapping to database tables
β β β βββ exceptions/ # Custom exceptions and global error handling
β β β βββ implementations/ # Service implementations
β β β βββ payloads/ # Data Transfer Objects (DTOs)
β β β βββ repositories/ # JPA repository interfaces
β β β βββ security/ # Security components (JWT, authentication filters, etc.)
β β β βββ services/ # Business logic interfaces and implementations
β β β βββ utils/ # Utility classes (constants, password encoder, etc.)
β β βββ resources/ # Application configurations (properties, logback, etc.)
βββ .mvn/ # Maven wrapper configuration
Note: The folder structure is simplified for clarity. Each directory contains related files that keep the codebase modular and maintainable.
- Java 17 or later
- Maven
- Docker (preferable, for containerized deployment)
-
Clone the repository:
git clone https://github.com/Yassinekrn/yassinekrn-real-time-api-spring-boot.git cd yassinekrn-real-time-api-spring-boot
-
Set up environment variables:
Create a
.env
file in the root directory and add the following:APP_JWT_SECRET=<your_secret_key> APP_JWT_EXPIRATION_IN_MS=<duration_in_milliseconds> # Uncomment one of the following lines based on your environment # OLLAMA_API_URL=http://localhost:11434/api/generate (for local development) # OLLAMA_API_URL=http://host.docker.internal:11434/api/generate (for Docker) OLLAMA_MODEL_NAME=<model_name> CORS_ALLOWED_ORIGINS=https://frontend1.com,https://frontend2.com (must be comma-separated)
-
Update
application.properties
:Before building the JAR file, ensure the following configuration is set:
spring.datasource.url=jdbc:mysql://localhost:3306/blogdb?useSSL=false&serverTimezone=UTC
For Docker deployment, comment the above line and use:
spring.datasource.url=jdbc:mysql://mysql-db:3306/blogdb
-
Build the project using Maven:
./mvn clean package
-
Build and run Docker containers:
docker-compose build --no-cache docker-compose down docker-compose up -d
- CORS: Configurable via environment variables to manage allowed domains. (refer to step 2 in the installation section)
- Security: JWT-based authentication is set up with role-based access. (only 2 roles are available by default:
ROLE_USER
andROLE_ADMIN
. also, please refer to step 2 in the installation section) - Rate Limiting: Implemented using Bucket4j to prevent API abuse. (set to 10 requests per minute by default, could be changed in
RateLimitingFilterConfig.java
).
All configurations are managed via application.properties
and the .env
file.
You can also check the utils/Constants.java
file for additional configuration constants.
If you want to add the admin role, you will need to manually insert it into the database.
Connect to the database and run the following query:
INSERT INTO roles(name) VALUES('ROLE_ADMIN');
Once the application is running, access the interactive API documentation via:
http://localhost:8080/swagger-ui.html
This documentation is generated using Swagger/OpenAPI and provides comprehensive details for each endpoint.
- Postman Collection: Import the provided Postman collection to test all endpoints.
- Swagger UI: Use the interactive Swagger UI for quick API testing and exploration.
- Docker: Use the provided
Dockerfile
anddocker-compose.yml
for containerized deployment. - Maven: Standard Maven build process is used for project packaging.
Contributions are welcome! Please fork the repository and submit a pull request with your improvements.
Distributed under the MIT License. See LICENSE
for more information.
For inquiries or support, please contact: [email protected] LinkedIn
Enjoy using the Spring Boot Real-Time API and happy coding! π
Made with β€οΈ by Yassine Krichen