# callicoder/spring-boot-file-upload-download-rest-api-example architecture

> Architecture diagram of the GitHub repository callicoder/spring-boot-file-upload-download-rest-api-example, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-05-08). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/callicoder/spring-boot-file-upload-download-rest-api-example
- Repository: https://github.com/callicoder/spring-boot-file-upload-download-rest-api-example

## Overview

##### Project type and purpose
- This is a monolithic, full-stack Spring Boot application whose sole purpose is to provide REST endpoints for uploading and downloading files, along with a minimal static front-end (HTML/JS/CSS) to demonstrate usage.
- The README confirms it is “Spring Boot File Upload / Download Rest API Example” and explains how to run a web server on port 8080 that stores files on the local filesystem.

##### File structure
- Top-level: standard Maven layout (pom.xml, mvnw, .mvn, src).
- src/main/java/com/example/filedemo:
- FileDemoApplication.java – Spring Boot entry point
- controller/FileController.java – REST API layer
- service/FileStorageService.java – business logic for storing/loading files
- exception (custom exception classes)
- payload/UploadFileResponse.java – DTO for upload responses
- property/FileStorageProperties.java – binds the upload directory property
- src/main/resources/static: contains index.html, main.js, main.css – a basic client UI
- application.properties: defines file.upload-dir
- No external database – uses local filesystem.

##### README for additional insights
- Describes a single REST API for upload (/upload) and download (/download/{filename}).
- Emphasizes configuration of a file.upload-dir.
- No mention of additional microservices or databases.

a. Main components
- Web Client (Browser) – loads static bundle (HTML, JS, CSS)
- Static Resource Server – Spring’s DefaultServlet to serve index.html + assets
- REST API Layer – FileController exposing /upload and /download endpoints via DispatcherServlet
- Service Layer – FileStorageService handling store/load logic
- Configuration – FileStorageProperties reads file.upload-dir
- Local File System – persistent storage for uploaded files
- Exception Handling Module – catches and maps FileStorageException/MyFileNotFoundException
b. Relationships & interactions
1. Browser → GET /index.html, main.js, main.css (Static Resource Server)
2. Browser → POST /upload (FileController) with multipart file
3. FileController → FileStorageService.storeFile(...) → writes file to FileSystem path from FileStorageProperties
4. FileController → GET /download/{filename} → FileStorageService.loadFileAsResource(...) → reads from FileSystem and returns Resource
5. Errors in service layer → thrown as FileStorageException/MyFileNotFoundException → global exception handler returns appropriate HTTP error
c. Architectural patterns & design principles
- Layered (Controller → Service → Infrastructure)
- Externalize configuration (FileStorageProperties + application.properties)
- DTO pattern for REST payload (UploadFileResponse)
- Exception translation to HTTP responses
d. Key technologies
- Spring Boot (Spring MVC, Spring @ConfigurationProperties)
- Maven (build lifecycle)
- Java 8+ I/O for filesystem storage
- Static front-end (vanilla JS/CSS/HTML)

##### Elements
- Front-end (e.g., light blue)
- Spring MVC layer (e.g., green)
- Service & config (e.g., yellow)
- Filesystem (e.g., gray)

## Diagram (Mermaid)

```mermaid
graph TB
    subgraph "Frontend Layer"
        Browser["Browser"]:::frontend
        StaticServer["Static Resource Server"]:::frontend
    end

    subgraph "Spring MVC Layer"
        EntryPoint["FileDemoApplication"]:::mvc
        FileController["FileController"]:::mvc
        ExceptionHandler["Global Exception Handler"]:::exception
    end

    subgraph "Service & Configuration"
        FileStorageService["FileStorageService"]:::service
        FileStorageProperties["FileStorageProperties"]:::config
    end

    subgraph "Persistence Layer"
        FileSystem["Local File System"]:::fs
    end

    Browser -->|"GET /index.html\nGET main.css\nGET main.js"| StaticServer
    StaticServer -->|serves assets| Browser

    Browser -->|"POST /upload"| FileController
    Browser -->|"GET /download/{filename}"| FileController

    FileController -->|"storeFile() call"| FileStorageService
    FileController -->|"loadFileAsResource() call"| FileStorageService

    FileStorageService -->|"reads upload-dir property"| FileStorageProperties
    FileStorageService -->|"write file to disk"| FileSystem
    FileStorageService -->|"read file from disk"| FileSystem

    FileStorageService -->|throws exceptions| ExceptionHandler
    ExceptionHandler -->|"maps to HTTP response"| FileController

    classDef frontend fill:#ADD8E6,stroke:#000,stroke-width:1px
    classDef mvc fill:#90EE90,stroke:#000,stroke-width:1px
    classDef service fill:#FFFFE0,stroke:#000,stroke-width:1px
    classDef config fill:#FFD700,stroke:#000,stroke-width:1px
    classDef fs fill:#D3D3D3,stroke:#000,stroke-width:1px
    classDef exception fill:#FFC0CB,stroke:#000,stroke-width:1px

    click Browser "https://github.com/callicoder/spring-boot-file-upload-download-rest-api-example/blob/master/src/main/resources/static/index.html"
    click StaticServer "https://github.com/callicoder/spring-boot-file-upload-download-rest-api-example/blob/master/src/main/resources/static/index.html"
    click Browser "https://github.com/callicoder/spring-boot-file-upload-download-rest-api-example/blob/master/src/main/resources/static/js/main.js"
    click Browser "https://github.com/callicoder/spring-boot-file-upload-download-rest-api-example/blob/master/src/main/resources/static/css/main.css"
    click EntryPoint "https://github.com/callicoder/spring-boot-file-upload-download-rest-api-example/blob/master/src/main/java/com/example/filedemo/FileDemoApplication.java"
    click FileController "https://github.com/callicoder/spring-boot-file-upload-download-rest-api-example/blob/master/src/main/java/com/example/filedemo/controller/FileController.java"
    click FileStorageService "https://github.com/callicoder/spring-boot-file-upload-download-rest-api-example/blob/master/src/main/java/com/example/filedemo/service/FileStorageService.java"
    click FileStorageProperties "https://github.com/callicoder/spring-boot-file-upload-download-rest-api-example/blob/master/src/main/java/com/example/filedemo/property/FileStorageProperties.java"
    click FileStorageProperties "https://github.com/callicoder/spring-boot-file-upload-download-rest-api-example/blob/master/src/main/resources/application.properties"
    click FileStorageException "https://github.com/callicoder/spring-boot-file-upload-download-rest-api-example/blob/master/src/main/java/com/example/filedemo/exception/FileStorageException.java"
    click MyFileNotFoundException "https://github.com/callicoder/spring-boot-file-upload-download-rest-api-example/blob/master/src/main/java/com/example/filedemo/exception/MyFileNotFoundException.java"
    click UploadFileResponse "https://github.com/callicoder/spring-boot-file-upload-download-rest-api-example/blob/master/src/main/java/com/example/filedemo/payload/UploadFileResponse.java"
    click Readme "https://github.com/callicoder/spring-boot-file-upload-download-rest-api-example/blob/master/Readme.md"
```
