An open API service indexing awesome lists of open source software.

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.

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)