Skip to content

A Spring Boot REST API for managing blog posts with JWT authentication, PostgreSQL, and Swagger documentation. Includes Docker deployment and live hosting on Render.com.

Notifications You must be signed in to change notification settings

shuvojoseph/BlogPostSpringBoot

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

29 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

📝 BlogPost Spring Boot API

A full-stack ready Spring Boot REST API for a blogging platform with JWT authentication, PostgreSQL database, Spring Data JPA, and Swagger API documentation powered by springdoc-openapi. This project is containerized with Docker and deployable to Render.com.


🚀 Features

  • User Authentication & Authorization

    • Registration and Login endpoints using JWT token-based authentication.
    • Public routes for login, register, and fetching all blogs.
    • All other routes are secured using Spring Security.
  • Blog Management

    • Create, Read, Update, Delete (CRUD) operations for blog posts.
    • Each post is associated with a registered user.
  • CORS Configuration

    • Configured to work seamlessly with a frontend React app.
    • Custom SecurityFilterChain ensures correct CORS and endpoint access rules.
  • API Documentation

    • Interactive API documentation using SpringDoc OpenAPI.
    • Available at /swagger-ui/index.html.
  • Database

    • PostgreSQL database integration via Spring Data JPA.
    • Configured with environment variables for flexible deployment.
  • Testing

    • Unit tests for both BlogService and BlogController using JUnit 5, Mockito, AssertJ, and MockMvc.

🛠️ Tech Stack

Layer Technologies
Backend Java 17, Spring Boot
Security Spring Security, JWT
Persistence Spring Data JPA, PostgreSQL
Documentation SpringDoc OpenAPI (Swagger)
Build Tool Maven
Testing JUnit 5, Mockito, AssertJ, MockMvc
Containerization Docker
Deployment Render.com

⚙️ Getting Started

1️⃣ Prerequisites

  • Java 17 or higher
  • Maven 3.9+
  • PostgreSQL installed locally
  • Docker Desktop (optional, for containerized run)

2️⃣ Clone the Repository

git clone https://github.com/shuvojoseph/BlogPostSpringBoot.git cd BlogPostSpringBoot

3️⃣ Database Configuration (Local)

Update src/main/resources/application.properties with your PostgreSQL credentials:

spring.datasource.url=jdbc:postgresql://localhost:5432/BlogDB spring.datasource.username=postgres spring.datasource.password=root spring.jpa.hibernate.ddl-auto=update spring.jpa.show-sql=true

Run PostgreSQL locally, then start the app:

./mvnw spring-boot:run

Visit Swagger UI at: 👉 http://localhost:8080/swagger-ui/index.html


🧪 Running Tests

Spring Boot automatically detects test classes under src/test.

Run all tests:

./mvnw test

Run only service tests:

./mvnw -Dtest=BlogServiceTest test

Run only controller tests:

./mvnw -Dtest=BlogControllerTest test

🐳 Docker Support

Dockerfile

A two-stage Docker build is used — first for compilation, then for runtime:

# ---- build stage ---- FROM maven:3.9.4-eclipse-temurin-17 as build WORKDIR /workspace COPY pom.xml mvnw ./ COPY .mvn .mvn RUN ./mvnw -B -f pom.xml dependency:go-offline COPY src ./src RUN ./mvnw -B -f pom.xml package -DskipTests # ---- runtime stage ---- FROM eclipse-temurin:17-jre-jammy WORKDIR /app ARG JAR_FILE=target/*.jar COPY --from=build /workspace/${JAR_FILE} app.jar EXPOSE 8080 ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app/app.jar"]

Run with Docker (Local DB)

docker build -t blogpost:local . docker run -p 8080:8080 \ -e PORT=8080 \ -e SPRING_DATASOURCE_URL="jdbc:postgresql://host.docker.internal:5432/BlogDB" \ -e SPRING_DATASOURCE_USERNAME=postgres \ -e SPRING_DATASOURCE_PASSWORD=root \ -e APP_JWT_SECRET="Q1kzRVMycnlIN2ZzR3dpamJaa2RwcU41aGlDUUhtZXc=" \ blogpost:local

Access the API at: 👉 http://localhost:8080/swagger-ui/index.html


🌐 Deployment to Render.com

1️⃣ Steps

  1. Push this project to your GitHub account.
  2. Log in to Render and click “New Web Service”.
  3. Select this GitHub repository.
  4. Add the following Environment Variables in Render’s dashboard:
Key Value
PORT 8080
SPRING_DATASOURCE_URL jdbc:postgresql://<render-db-host>:5432/<dbname>
SPRING_DATASOURCE_USERNAME <dbuser>
SPRING_DATASOURCE_PASSWORD <dbpass>
APP_JWT_SECRET change-this-to-a-strong-secret

2️⃣ Example Render PostgreSQL Details

Database name: blogdb_xz40 User: postgresuser Hostname: dpg-d3ck3ondiees738267q0-a Port: 5432 External URL: postgresql://postgresuser:<password>@dpg-d3ck3ondiees738267q0-a.oregon-postgres.render.com/blogdb_xz40 

✅ Live API

Base URL: 🔗 https://blogpostspringboot.onrender.com/

Swagger Docs: 🔗 https://blogpostspringboot.onrender.com/swagger-ui/index.html


🧱 Project Structure

src └── main ├── java/com/example/BlogPostSpringBoot │ ├── controller/ → REST controllers │ ├── service/ → Business logic │ ├── repository/ → Spring Data JPA repositories │ ├── model/ → Entity classes │ ├── security/ → JWT & SecurityFilterChain │ └── BlogPostSpringBootApplication.java └── resources/ ├── application.properties └── static/templates (if used) 

🧩 Future Improvements

  • Add Flyway migrations for auto schema setup.
  • Add pagination and search to blog endpoints.
  • Implement comment system.
  • Integrate email verification on registration.
  • Deploy a frontend React app connected to this API.

👨‍💻 Author

Shuvo Joseph Founder of DeshiIT 💼 Full-Stack & iOS Developer | Backend (Spring Boot, .NET Core, NestJS, Laravel) 📧 Contact: LinkedIn

About

A Spring Boot REST API for managing blog posts with JWT authentication, PostgreSQL, and Swagger documentation. Includes Docker deployment and live hosting on Render.com.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published