Skip to content

MuradSharifzada/Notification-MS-Java

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Notification Management Microservice

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).

Tech Stack

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

Architecture

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

API Endpoints

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>

Request / Response Examples

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"
}

Data Model

Notification Entity

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

Getting Started

Prerequisites

  • Java 21
  • PostgreSQL instance
  • Gmail account with an App Password (for SMTP)

Configuration

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 & Run

# Build the project
./gradlew build

# Run the application
./gradlew bootRun

The service starts on http://localhost:8081.

API Documentation

Once running, Swagger UI is available at:

http://localhost:8081/swagger-ui.html

Project Structure

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

License

This project is licensed under the Apache 2.0 License.

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages