A Spring Boot microservice for managing and sending email notifications. Part of a microservice architecture, it provides REST APIs to create, retrieve, and send notifications to users via email (Gmail SMTP).
| Technology | Version | Purpose |
|---|---|---|
| Java | 21 | Language |
| Spring Boot | 3.3.5 | Application framework |
| Spring Cloud OpenFeign | 2023.0.3 | Inter-service communication |
| Spring Data JPA | 3.3.5 | Database ORM |
| Spring Boot Mail | 3.3.5 | Email sending |
| Spring Boot Actuator | 3.3.5 | Monitoring & management |
| PostgreSQL | Latest | Relational database |
| MapStruct | 1.6.2 | DTO ↔ Entity mapping |
| Lombok | Latest | Boilerplate reduction |
| SpringDoc OpenAPI | 2.1.0 | Swagger UI / API docs |
| Gradle | 8.10.2 | Build tool |
Controller (REST)
│
▼
Service Layer (Business Logic)
├──► NotificationRepository (JPA / PostgreSQL)
├──► EmailNotificationService (JavaMailSender / Gmail SMTP)
└──► UserClient (Feign → tutorial-service)
Key components:
- NotificationController – REST endpoints under
/api - NotificationServiceImpl – orchestrates persistence and email dispatch
- EmailNotificationService – sends emails via Gmail SMTP
- UserClient – Feign client that fetches user emails from a companion service
- NotificationMapper – MapStruct mapper between DTOs and entities
| Method | Endpoint | Description | Request Body | Response |
|---|---|---|---|---|
POST |
/api/notifications/send |
Send a notification | NotificationRequest |
NotificationResponse |
GET |
/api/{id} |
Get notification by ID | – | Notification |
GET |
/api/user/{userId} |
Get notifications for a user | – | List<Notification> |
POST /api/notifications/send
// Request
{
"recipient": "user@example.com",
"subject": "Account Update",
"message": "Your account has been updated successfully."
}
// Response (200 OK)
{
"status": "Pending",
"recipient": "user@example.com",
"subject": "Account Update",
"message": "Your account has been updated successfully."
}GET /api/{id}
// Response (200 OK)
{
"id": 1,
"userId": null,
"message": "Your account has been updated successfully.",
"status": "Pending",
"recipient": "user@example.com",
"sentTime": "2024-11-10T14:30:00"
}| Field | Type | Description |
|---|---|---|
id |
Long |
Auto-generated primary key |
userId |
Long |
Associated user ID |
message |
String |
Notification message body |
status |
String |
Status (e.g. Pending) |
recipient |
String |
Recipient email address |
sentTime |
LocalDateTime |
Timestamp of when the notification was sent |
- Java 21
- PostgreSQL instance
- Gmail account with an App Password (for SMTP)
Edit src/main/resources/application.properties with your environment values:
# Server
server.port=8081
# PostgreSQL
spring.datasource.url=jdbc:postgresql://localhost:5432/notification_db
spring.datasource.username=<your-db-username>
spring.datasource.password=<your-db-password>
spring.datasource.driver-class-name=org.postgresql.Driver
spring.jpa.hibernate.ddl-auto=update
# Gmail SMTP
spring.mail.host=smtp.gmail.com
spring.mail.port=587
spring.mail.username=<your-email>
spring.mail.password=<your-app-password>
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
# Eureka (Service Discovery)
eureka.client.service-url.defaultZone=http://localhost:8761/eureka
eureka.client.register-with-eureka=true
eureka.client.fetch-registry=true# Build the project
./gradlew build
# Run the application
./gradlew bootRunThe service starts on http://localhost:8081.
Once running, Swagger UI is available at:
http://localhost:8081/swagger-ui.html
src/main/java/com/example/notification_managements/
├── NotificationManagementMsApplication.java # Entry point
├── client/
│ └── UserClient.java # Feign client for user service
├── config/
│ └── SwaggerConfig.java # OpenAPI configuration
├── controller/
│ └── NotificationController.java # REST endpoints
├── dto/
│ ├── NotificationRequest.java # Inbound DTO
│ └── NotificationResponse.java # Outbound DTO
├── exception/
│ ├── NotificationNotFoundException.java # 404 exception
│ └── NotificationServiceException.java # 500 exception
├── mapper/
│ └── NotificationMapper.java # MapStruct mapper
├── model/
│ └── Notification.java # JPA entity
├── repository/
│ └── NotificationRepository.java # Spring Data JPA repository
└── service/
├── NotificationService.java # Service interface
└── impl/
├── EmailNotificationService.java # Email sender
├── EmailSenderServices.java # Alternative email utility
└── NotificationServiceImpl.java # Core business logic
This project is licensed under the Apache 2.0 License.