https://github.com/Yassinekrn/Advanced-Spring-Boot-Blog-API
A scalable Spring Boot Blog REST API for efficient content management, featuring secure authentication, post and comment management, and detailed API docs. Built with industry best practices, it offers clean, maintainable code, ideal for developers seeking a high-quality Spring Boot project.
https://github.com/Yassinekrn/Advanced-Spring-Boot-Blog-API
blog-platform crud docker exception-handling jwt-authentication model-mapper pagination restful-api role-based-access-control search-functionality spring-boot swagger
Last synced: 3 months ago
JSON representation
A scalable Spring Boot Blog REST API for efficient content management, featuring secure authentication, post and comment management, and detailed API docs. Built with industry best practices, it offers clean, maintainable code, ideal for developers seeking a high-quality Spring Boot project.
- Host: GitHub
- URL: https://github.com/Yassinekrn/Advanced-Spring-Boot-Blog-API
- Owner: Yassinekrn
- License: mit
- Created: 2025-01-26T13:46:38.000Z (12 months ago)
- Default Branch: main
- Last Pushed: 2025-02-02T17:23:26.000Z (11 months ago)
- Last Synced: 2025-05-07T15:08:36.341Z (8 months ago)
- Topics: blog-platform, crud, docker, exception-handling, jwt-authentication, model-mapper, pagination, restful-api, role-based-access-control, search-functionality, spring-boot, swagger
- Language: Java
- Homepage:
- Size: 82 KB
- Stars: 3
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Spring Boot Real-Time API
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 โจ](#features)
- [Project Structure ๐](#project-structure)
- [Getting Started ๐](#getting-started)
- [Configuration โ๏ธ](#configuration)
- [API Documentation ๐](#api-documentation)
- [Testing ๐งช](#testing)
- [Deployment ๐](#deployment)
- [Contributing ๐ค](#contributing)
- [License ๐](#license)
---
## Features โจ
### User Authentication and Authorization ๐
- **JWT-based authentication** for secure stateless sessions.
- **Role-based access control** with Admin and User roles.
- Endpoints for **user registration** and **login**.
### Post Management ๐
- **CRUD operations** for blog posts.
- **Pagination & sorting** for efficient listing.
- **Search functionality** to find posts by title or description.
#### AI-Powered Enhancements ๐ค
- **AI-driven content summarization** using locally hosted ollama LLM.
### Comment Management ๐ฌ
- **CRUD operations** for comments.
- Fetch comments by **post ID**.
- Ensure comments belong to the correct post.
- **Role-based permissions** for comment operations.
### User Management ๐ฅ
- **CRUD operations** for user profiles.
- **Role assignment** for better access control.
- Retrieve detailed user information.
### Security ๐
- **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.
### API Documentation ๐
- **Swagger/OpenAPI integration** for interactive API docs.
- Detailed annotations and summaries for each endpoint.
### Configuration and Environment Management โ๏ธ
- **Externalized configuration** using a `.env` file and environment-specific properties.
- Easy management of database connections and other settings.
### Exception Handling ๐จ
- Custom exception classes for precise error reporting.
- Global exception handler to manage API error responses uniformly.
### Database Interaction ๐๏ธ
- **JPA repositories** for seamless database operations.
- Entity classes with **Hibernate ORM** for effective object-relational mapping.
### Utility Services ๐ ๏ธ
- **ModelMapper** for DTO and entity mapping.
- **BCrypt** for secure password encoding.
### Testing ๐งช
- **Postman collections** provided for API testing.
- **Swagger UI** for interactive endpoint testing.
### Deployment and Build ๐
- **Docker support** with a Dockerfile and docker-compose configuration.
- **Maven build configuration** for streamlined project management.
### Logging and Monitoring ๐
- Configurable logging levels using **SLF4J** with **Logback**.
- **Actuator endpoints** for health checks and application metrics.
### Industry Best Practices ๐
- Clean, maintainable code following design patterns.
- Well-structured project organization for scalability and readability.
---
## Project Structure ๐
```
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._
---
## Getting Started ๐
### Prerequisites
- **Java 17** or later
- **Maven**
- **Docker** (preferable, for containerized deployment)
### Installation
1. **Clone the repository:**
```bash
git clone https://github.com/Yassinekrn/yassinekrn-real-time-api-spring-boot.git
cd yassinekrn-real-time-api-spring-boot
```
2. **Set up environment variables:**
Create a `.env` file in the root directory and add the following:
```env
APP_JWT_SECRET=
APP_JWT_EXPIRATION_IN_MS=
# 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=
CORS_ALLOWED_ORIGINS=https://frontend1.com,https://frontend2.com (must be comma-separated)
```
3. **Update `application.properties`:**
Before building the JAR file, ensure the following configuration is set:
```properties
spring.datasource.url=jdbc:mysql://localhost:3306/blogdb?useSSL=false&serverTimezone=UTC
```
For Docker deployment, comment the above line and use:
```properties
spring.datasource.url=jdbc:mysql://mysql-db:3306/blogdb
```
4. **Build the project using Maven:**
```bash
./mvn clean package
```
5. **Build and run Docker containers:**
```bash
docker-compose build --no-cache
docker-compose down
docker-compose up -d
```
---
## Configuration โ๏ธ
### Environment Variables and custom configurations
- **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` and `ROLE_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._
### Note on Admin Role ๐ก๏ธ
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:
```sql
INSERT INTO roles(name) VALUES('ROLE_ADMIN');
```
---
## API Documentation ๐
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.
---
## Testing ๐งช
- **Postman Collection:** Import the provided Postman collection to test all endpoints.
- **Swagger UI:** Use the interactive Swagger UI for quick API testing and exploration.
---
## Deployment ๐
- **Docker:** Use the provided `Dockerfile` and `docker-compose.yml` for containerized deployment.
- **Maven:** Standard Maven build process is used for project packaging.
---
## Contributing ๐ค
Contributions are welcome! Please fork the repository and submit a pull request with your improvements.
---
## License ๐
Distributed under the MIT License. See `LICENSE` for more information.
---
## Contact ๐
For inquiries or support, please contact:
[krichenyassine22@gmail.com](mailto:krichenyassine22@gmail.com)
[LinkedIn](https://www.linkedin.com/in/krichenyassine/)
---
Enjoy using the **Spring Boot Real-Time API** and happy coding! ๐
---
Made with โค๏ธ by [Yassine Krichen](https://github.com/Yassinekrn)