Skip to content

Haseeb-Khalil/CRUD_API_With_SpringBoot_PostgreSQL

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

23 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Spring Boot CRUD API (H2 by default, PostgreSQL via profile)

A Spring Boot 3 REST API that performs CRUD operations on a Student resource through Spring Data JPA. Runs out of the box on an in-memory H2 database, with a postgres profile for switching to PostgreSQL.

What this demonstrates

  • Building a REST API with Spring Boot 3 on Java 17.
  • Persisting entities with Spring Data JPA and Hibernate.
  • Layered architecture: Controller for HTTP, Service for business logic, Repository for persistence.
  • Bootstrapping seed data via a @Configuration class.
  • Switching between H2 (development) and PostgreSQL (production) through Spring profiles.

Tech stack

  • Java 17
  • Spring Boot 3.2.5 (Web, Data JPA)
  • H2 (default), PostgreSQL (optional)
  • Maven

Project structure

src/main/java/com/example/demo
├── DemoApplication.java         # Spring Boot entry point
└── student
    ├── Student.java             # JPA entity
    ├── StudentRepository.java   # Spring Data JPA repository
    ├── StudentService.java      # Business logic
    ├── StudentController.java   # REST endpoints
    └── StudentConfig.java       # Seed data on startup

Run it on your machine

Prerequisites

  • Java 17 or newer (java -version)
  • No need to install Maven, the project includes the Maven wrapper

Steps

# 1. Clone
git clone https://github.com/Haseeb-Khalil/CRUD_API_With_SpringBoot_PostgreSQL.git
cd CRUD_API_With_SpringBoot_PostgreSQL

# 2. Run (uses H2 in-memory by default, no setup required)
./mvnw spring-boot:run
# On Windows:  mvnw.cmd spring-boot:run

The app starts on http://localhost:8080.

Try the endpoints

# List all students (seed data)
curl http://localhost:8080/api/v1/student

# Create a student
curl -X POST http://localhost:8080/api/v1/student \
     -H "Content-Type: application/json" \
     -d '{"name":"Test","email":"test@example.com","dob":"2000-01-01"}'

# Update
curl -X PUT "http://localhost:8080/api/v1/student/1?name=Updated"

# Delete
curl -X DELETE http://localhost:8080/api/v1/student/1

H2 console is available at http://localhost:8080/h2 (JDBC URL: jdbc:h2:mem:studentdb, user: sa, no password).

Switch to PostgreSQL

# Start with the postgres profile
./mvnw spring-boot:run -Dspring-boot.run.profiles=postgres

# Override connection details with environment variables if needed
DB_URL=jdbc:postgresql://localhost:5432/student \
DB_USERNAME=postgres \
DB_PASSWORD=postgres \
./mvnw spring-boot:run -Dspring-boot.run.profiles=postgres

Why I built this

Hands-on practice for the patterns I use day to day at Capgemini on the HMRC Enterprise Integration Services (EIS) programme: REST endpoints, JPA persistence, Maven builds and a clear separation of concerns.

About

Spring Boot 3 REST API with JPA and PostgreSQL. Clean Controller / Service / Repository layering on Java 17.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages