|
1 | 1 | # Spring Boot Template |
2 | 2 |
|
3 | 3 | ## Description |
4 | | -A template for Spring Boot projects. This template includes the following features: |
5 | | -- Spring Boot 3.2.3 (latest 3.x LTS, updated for security and new features) |
6 | | -- Java 21 (latest LTS, required for Spring Boot 3.2+) |
7 | | -- Spring Security with JWT authentication and authorization also with refresh token |
8 | | -- Swagger UI for API documentation and testing (http://localhost:8080/api/v1/swagger-ui/index.html) with authentication and authorization support |
9 | | -- Spring Data JPA with PostgreSQL database integration |
10 | | -- Exception handling mechanism with custom exceptions and exception handlers using `@ControllerAdvice` |
11 | | -- Base entity and base DTO for common fields |
12 | | -- Base service for common business logic |
13 | | -- Custom `@PreAuthorize` annotation for authorization |
14 | | -- Lombok for reducing boilerplate code |
15 | | -- MapStruct for mapping DTOs to entities and vice versa |
16 | | -- **Docker Compose support for easy containerized development** |
17 | | -- Redis integration for caching and session management (configurable via environment variables) |
18 | | - |
19 | | -> **Note:** This template is up-to-date with Spring Boot 3.2.3 and Java 21. Ensure your local environment supports Java 21 for development and builds. |
20 | | -
|
21 | | -## How to run |
| 4 | +A robust template for Spring Boot projects with modern best practices and production-ready integrations. |
| 5 | + |
| 6 | + |
| 7 | + |
| 8 | + |
| 9 | +### Key Features |
| 10 | +- **Spring Boot 3.2.3** (Java 21 LTS) |
| 11 | +- **JWT Authentication & Authorization** (with refresh tokens) |
| 12 | +- **Swagger UI** for API documentation and testing ([http://localhost:8080/api/v1/swagger-ui/index.html](http://localhost:8080/api/v1/swagger-ui/index.html)) |
| 13 | +- **PostgreSQL** integration (via Docker Compose) |
| 14 | +- **Redis** for caching and session management |
| 15 | +- **Kafka & Zookeeper** for message brokering (via Docker Compose) |
| 16 | +- **Role-based Security** with Spring Security |
| 17 | +- **Rate Limiting** |
| 18 | +- **Exception Handling** with `@ControllerAdvice` |
| 19 | +- **Lombok** and **MapStruct** for reduced boilerplate |
| 20 | +- **Environment Isolation**: Easily run multiple environments (dev, test, prod) in parallel using Docker Compose project names and override files |
| 21 | +- **Configurable Logging** (including Kafka) |
| 22 | +- **Unit & Integration Tests** for key services and controllers |
| 23 | + |
| 24 | +> **Note:** Java 21 is required. Ensure your local environment supports Java 21 for development and builds. |
| 25 | +
|
| 26 | +--- |
| 27 | + |
| 28 | +## How to Run |
22 | 29 |
|
23 | 30 | ### Using Docker Compose (Recommended) |
24 | 31 | 1. Clone this repository |
25 | | -2. Make sure Docker and Docker Compose are installed on your system |
26 | | -3. Run the following command in the project root: |
| 32 | +2. Ensure Docker and Docker Compose are installed |
| 33 | +3. For a default environment, run: |
27 | 34 | ```sh |
28 | 35 | docker compose up -d |
29 | 36 | ``` |
30 | | -4. The application will be available at [http://localhost:8080/api/v1/swagger-ui/index.html](http://localhost:8080/api/v1/swagger-ui/index.html) |
31 | | -5. PostgreSQL will be available at port 5432 (default credentials are set in `docker-compose.yml` and can be overridden with environment variables) |
| 37 | +4. For multiple environments (e.g., test and prod) in parallel: |
| 38 | + ```sh |
| 39 | + docker-compose -f docker-compose.yml -f docker-compose.test.override.yml --env-file env.test -p myapp_test up -d |
| 40 | + docker-compose -f docker-compose.yml -f docker-compose.prod.override.yml --env-file env.prod -p myapp_prod up -d |
| 41 | + ``` |
| 42 | + - This will create isolated containers, networks, and volumes for each environment. |
| 43 | + - Make sure to set different ports in override files to avoid conflicts. |
| 44 | +5. The application will be available at [http://localhost:8080/api/v1/swagger-ui/index.html](http://localhost:8080/api/v1/swagger-ui/index.html) (or the port you set). |
32 | 45 |
|
33 | 46 | ### Using Maven (Local Development) |
34 | 47 | 1. Clone this repository |
35 | | -2. Create a PostgreSQL database or use the provided Docker Compose setup |
36 | | -3. Change the database configuration in `application.properties` if needed |
37 | | -4. Make sure you have redis and postgresql running locally or use the Docker Compose setup |
38 | | -4. Run the application with: |
| 48 | +2. Set up PostgreSQL and Redis (or use Docker Compose) |
| 49 | +3. Adjust `application.properties` as needed |
| 50 | +4. Run: |
39 | 51 | ```sh |
40 | 52 | mvn spring-boot:run |
41 | 53 | ``` |
42 | | -5. Open [http://localhost:8080/api/v1/swagger-ui/index.html](http://localhost:8080/api/v1/swagger-ui/index.html) in your browser |
| 54 | +5. Open [http://localhost:8080/api/v1/swagger-ui/index.html](http://localhost:8080/api/v1/swagger-ui/index.html) |
43 | 55 |
|
44 | | -### API Usage |
45 | | -1. Register a new user |
46 | | -2. Login with the registered user |
47 | | -3. Copy the JWT token from the response |
48 | | -4. Click the `Authorize` button in the Swagger UI |
49 | | -5. Paste the JWT token in the `Value` field without the `Bearer` prefix |
50 | | -6. Click the `Authorize` button |
51 | | -7. Now you can test the API endpoints |
52 | | -8. To test the refresh token endpoint, click the `Authorize` button again and paste the refresh token in the `Value` field without the `Bearer` prefix |
| 56 | +--- |
53 | 57 |
|
54 | | - |
55 | | - |
56 | | - |
57 | | - |
58 | | -## Environment Variables |
59 | | -- You can override default database credentials and other settings using environment variables in `docker-compose.yml` or by creating a `.env` file in the project root. |
| 58 | +## Kafka Messaging |
| 59 | +- Kafka and Zookeeper are included in Docker Compose for local development. |
| 60 | +- REST endpoint `/kafka/send` allows sending messages to Kafka, with optional scheduling for future delivery. |
| 61 | +- Kafka consumer logs received messages. |
| 62 | +- Kafka logging is configurable in `application.properties`. |
60 | 63 |
|
61 | | -### Environment Variables for Redis |
| 64 | +--- |
62 | 65 |
|
63 | | -You can configure Redis connection using environment variables (recommended for Docker Compose): |
| 66 | +## Environment Configuration |
| 67 | +- Use `.env` files for environment-specific variables. |
| 68 | +- Use Docker Compose override files for port and resource separation. |
| 69 | +- Example for test environment: |
| 70 | + ```sh |
| 71 | + docker-compose -f docker-compose.yml -f docker-compose.test.override.yml --env-file env.test -p myapp_test up -d |
| 72 | + ``` |
64 | 73 |
|
65 | | -- `SPRING_DATA_REDIS_HOST` (default: `redis`) |
66 | | -- `SPRING_DATA_REDIS_PORT` (default: `6379`) |
| 74 | +--- |
67 | 75 |
|
68 | | -Example in `docker-compose.yml`: |
| 76 | +## API Usage |
| 77 | +1. Register a new user |
| 78 | +2. Login and obtain a JWT token |
| 79 | +3. Use the token in Swagger UI via the `Authorize` button |
| 80 | +4. Access secured endpoints (e.g., Kafka messaging requires ADMIN role) |
69 | 81 |
|
70 | | -```yaml |
71 | | -app: |
72 | | - # ...existing code... |
73 | | - environment: |
74 | | - SPRING_DATA_REDIS_HOST: redis |
75 | | - SPRING_DATA_REDIS_PORT: 6379 |
76 | | - # ...other env vars... |
77 | | -``` |
| 82 | +--- |
78 | 83 |
|
79 | | -Redis will be available at port 6379 by default. |
| 84 | +## Project Abilities Summary |
| 85 | +- Modular REST API with security, rate limiting, and exception handling |
| 86 | +- PostgreSQL, Redis, Kafka, and Zookeeper integration (all containerized) |
| 87 | +- Role-based access control |
| 88 | +- Swagger/OpenAPI documentation |
| 89 | +- Environment isolation for dev, test, and prod |
| 90 | +- Configurable logging |
| 91 | +- Unit and integration tests |
80 | 92 |
|
81 | | -## Database Configuration |
| 93 | +--- |
82 | 94 |
|
83 | | -- To connect to your PostgreSQL database: |
84 | | - - Host: `db` |
85 | | - - Port: `5432` |
86 | | - - Username: as set in `docker-compose.yml` (default: `development`) |
87 | | - - Password: as set in `docker-compose.yml` (default: `T3st!ng`) |
| 95 | +For more details, see the code and comments, or open an issue for questions! |
88 | 96 |
|
89 | | -## Contribute to this project |
90 | | -If you want to contribute to this project, please create a pull request. |
|
0 commit comments