Skip to content

m4n50/springboot-blog-api

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

19 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Spring Boot Blog API

A professional RESTful Blog API built with Spring Boot 3.3.4 and Java 21. This project demonstrates modern backend development practices with clean architecture, comprehensive testing, and production-ready features.

πŸš€ Features

  • Complete CRUD Operations for blog posts
  • RESTful API Design with proper HTTP status codes
  • PostgreSQL Database with JPA/Hibernate
  • Comprehensive Error Handling with custom exceptions
  • API Documentation with Swagger/OpenAPI 3
  • Data Validation using Bean Validation
  • Unit & Integration Testing with JUnit 5 and REST Assured
  • Docker Support for database setup
  • Clean Architecture (Controller β†’ Service β†’ Repository)

πŸ› οΈ Tech Stack

  • Java 21 - Latest LTS version
  • Spring Boot 3.3.4 - Main framework
  • Spring Data JPA - Database operations
  • PostgreSQL - Production database
  • H2 Database - Testing database
  • Maven - Dependency management
  • Lombok - Reduce boilerplate code
  • SpringDoc OpenAPI - API documentation
  • Docker - Database containerization

πŸ“‹ Prerequisites

Before running this application, make sure you have:

  • Java 21+ installed
  • Maven 3.6+ installed
  • Docker and Docker Compose installed
  • Git installed

Lombok Setup(Intellij IDEA)

If you see "red methods" errors:

  1. Enable annotation processing in IntelliJ settings
  2. Invalidate caches and restart
  3. Run mvn clean compile

πŸ—οΈ Project Structure

src/
β”œβ”€β”€ main/
β”‚   β”œβ”€β”€ java/com/blog/blogapi/
β”‚   β”‚   β”œβ”€β”€ config/     # Comprehensive API documentation
β”‚   β”‚   β”œβ”€β”€ controller/ # REST controllers
β”‚   β”‚   β”œβ”€β”€ DTO/        # Data transfer objects
β”‚   β”‚   β”œβ”€β”€ exception/  # Custom exceptions & error handling
β”‚   β”‚   β”œβ”€β”€ mapper/     # DTO mappers transforming data between different object representations 
β”‚   β”‚   β”œβ”€β”€ model/      # Entities
β”‚   β”‚   β”œβ”€β”€ service/    # Business logic
β”‚   β”‚   β”œβ”€β”€ repository/ # Data access layer
β”‚   β”‚   └── BlogapiApplication.java
β”‚   └── resources/
β”‚       β”œβ”€β”€ application.properties
└── test/               # Unit and integration tests

πŸš€ Quick Start

1. Clone the Repository

git clone https://github.com/m4n50/springboot-blog-api.git
cd springboot-blog-api

2. Start PostgreSQL with Docker

# Create and start PostgreSQL container
docker run --name blog-postgres \
  -e POSTGRES_DB=blogapi \
  -e POSTGRES_USER=bloguser \
  -e POSTGRES_PASSWORD=blogpass \
  -p 5432:5432 \
  -d postgres:15

# Verify container is running
docker ps

3. Configure Database (Optional)

Update src/main/resources/application.properties if needed:

spring.datasource.url=jdbc:postgresql://localhost:5432/blogapi
spring.datasource.username=bloguser
spring.datasource.password=blogpass

4. Build and Run

# Build the project
mvn clean compile

# Run tests
mvn test

# Start the application
mvn spring-boot:run

The application will start on http://localhost:8080

πŸ“š API Documentation

Once the application is running, you can access:

πŸ”Œ API Endpoints

Blog Posts

Method Endpoint Description
GET /api/posts Get all blog posts
GET /api/posts/{id} Get a specific post
POST /api/posts Create a new post
PUT /api/posts/{id} Update an existing post
DELETE /api/posts/{id} Delete a post

Example Request/Response

Create a Post (POST /api/posts)

{
  "title": "Getting Started with Spring Boot",
  "content": "Spring Boot makes it easy to create stand-alone, production-grade applications...",
  "author": "John Doe"
}

Response

{
  "id": 1,
  "title": "Getting Started with Spring Boot",
  "content": "Spring Boot makes it easy to create stand-alone, production-grade applications...",
  "author": "John Doe",
  "createdAt": "2024-09-09T10:30:00",
  "updatedAt": "2024-09-09T10:30:00"
}

πŸ§ͺ Testing

Run the test suite:

# Run all tests
mvn test

# Run only unit tests
mvn test -Dtest="*Test"

# Run only integration tests
mvn test -Dtest="*IT"

# Generate test coverage report
mvn jacoco:report

🐳 Docker Support

Database Only (Current Setup)

docker run --name blog-postgres \
  -e POSTGRES_DB=blogapi \
  -e POSTGRES_USER=bloguser \
  -e POSTGRES_PASSWORD=blogpass \
  -p 5432:5432 \
  -d postgres:15

Full Application (Future Enhancement)

# Build application image
docker build -t blog-api .

# Run with Docker Compose
docker-compose up -d

🌟 Key Features Demonstrated

1. Clean Architecture

  • Separation of Concerns: Controllers handle HTTP, Services contain business logic, Repositories manage data
  • Dependency Injection: Proper use of Spring's IoC container
  • Single Responsibility: Each class has a focused purpose

2. Error Handling

  • Global Exception Handler: Centralized error handling
  • Custom Exceptions: Domain-specific error types
  • Proper HTTP Status Codes: RESTful error responses

3. Data Validation

  • Bean Validation: Input validation using annotations
  • Custom Validators: Business rule validation
  • Error Messages: User-friendly validation feedback

4. Testing Strategy

  • Unit Tests: Test individual components in isolation
  • Integration Tests: Test component interactions
  • REST Assured: API endpoint testing

πŸš€ Future Enhancements

  • User Authentication & Authorization (JWT)
  • User Roles (Admin, Author, Reader)
  • Post Categories & Tags
  • Pagination & Sorting
  • Search Functionality
  • File Upload for Images
  • Caching with Redis
  • API Versioning
  • CI/CD Pipeline

🀝 Contributing

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/new-feature)
  3. Commit your changes (git commit -am 'Add new feature')
  4. Push to the branch (git push origin feature/new-feature)
  5. Create a Pull Request

πŸ“„ License

This project is licensed under the MIT License - see the LICENSE file for details.

πŸ‘¨β€πŸ’» Author

Bruno Manso - Backend Developer

πŸ™ Acknowledgments

  • Spring Boot team for the excellent framework
  • PostgreSQL community for the robust database
  • Docker for simplifying development environments

⭐ Star this repository if it helped you learn Spring Boot!

About

A RESTful Blog API built with Spring Boot

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages