Project‐Architecture - ddeverse/ddeverse-pep-spring-project GitHub Wiki

Project Architecture

This page outlines the architecture and design principles behind the Spring Social Media Blog API project.

Overview

The application follows a three-layer architecture, a common pattern in Spring Boot applications::

Client → Controller → Service → Repository → Database

Each layer has a distinct responsibility:

  • Controller Layer: Handles incoming HTTP requests and delegates to the service layer.
  • Service Layer: Contains business logic and validation rules.
  • Repository Layer: Interacts with the database using Spring Data JPA.
  • Entity Layer: Defines the data model and its mapping to relational tables.

Package Structure

src/
└── main/
    ├── java/com/example/
    │   ├── controller/
    │   │   └── SocialMediaController.java
    │   ├── entity/
    │   │   ├── Account.java
    │   │   └── Message.java
    │   ├── exception/
    │   │   └── exception.txt
    │   ├── repository/
    │   │   ├── AccountRepository.java
    │   │   └── MessageRepository.java
    │   ├── service/
    │   │   ├── AccountService.java
    │   │   ├── MessageService.java
    │   │   └── SocialMediaApp.java
    └── resources/
        ├── application.properties
        └── data.sql

Responsibilities by Layer

Controller Layer

  • Annotated with @RestController.
  • Handles HTTP routes using @GetMapping and @PostMapping.
  • Converts request/response bodies via Spring’s Jackson integration.
  • Delegates business logic to services.

Service Layer

  • Contains core application logic and validation rules.
  • Acts as a bridge between controller and repository layers.
  • Throws exceptions for invalid operations.

Repository Layer

  • Uses Spring Data JPA repositories.
  • Extends JpaRepository to provide standard CRUD operations.
  • Defines query methods via method naming or @Query annotations.

Entity Layer

  • Contains @Entity classes mapped to database tables.
  • Uses annotations like @Id, @GeneratedValue, @Column, and @ManyToOne.

Configuration and Resources

application.properties

  • Spring Boot application configuration.
  • Typically includes:
    • DB connection info
    • JPA/Hibernate settings
    • Server port, logging configs

data.sql

  • Executed at startup by Spring Boot to initialize schema/data.
  • Inserts default rows into the account and message tables.

Database Schema

Account Table

account_id INTEGER PRIMARY KEY AUTO_INCREMENT,
username VARCHAR(255) UNIQUE,
password VARCHAR(255)

Message Table

message_id INTEGER PRIMARY KEY AUTO_INCREMENT,
posted_by INTEGER,
message_text VARCHAR(255),
time_posted_epoch BIGINT,
FOREIGN KEY (posted_by) REFERENCES Account(account_id)