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.
- Building a REST API with Spring Boot 3 on Java 17.
- Persisting entities with Spring Data JPA and Hibernate.
- Layered architecture:
Controllerfor HTTP,Servicefor business logic,Repositoryfor persistence. - Bootstrapping seed data via a
@Configurationclass. - Switching between H2 (development) and PostgreSQL (production) through Spring profiles.
- Java 17
- Spring Boot 3.2.5 (Web, Data JPA)
- H2 (default), PostgreSQL (optional)
- Maven
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
- Java 17 or newer (
java -version) - No need to install Maven, the project includes the Maven wrapper
# 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:runThe app starts on http://localhost:8080.
# 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/1H2 console is available at http://localhost:8080/h2 (JDBC URL: jdbc:h2:mem:studentdb, user: sa, no password).
# 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=postgresHands-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.