This is a Spring Boot REST API project that connects to a PostgreSQL database and return two endpoints in json listing all subjects associated with Software Engineering programme and list of students studying the programme respectively. Follow the instructions below to set up and run the project on your local machine.
Before you start, ensure you have the following installed:
-
Java 17 or later
-
Download from Oracle.
-
Verify installation:
java -version
-
-
Maven 3.8+ (for building the project)
-
Download from Maven.
-
Verify installation:
mvn -version
-
-
PostgreSQL 14+ (or use a cloud-hosted PostgreSQL instance)
- Install from PostgreSQL Official Site.
- Ensure the database is running and accessible.
-
Git (to clone the repository)
-
Download from Git.
-
Verify installation:
git --version
-
To get a copy of this project, run:
git clone https://github.com/Ymwemanoor/springboot-api.git
cd springboot-apiEnsure you have a PostgreSQL database created before running the application.
Run the following SQL to create a database:
CREATE DATABASE rest-api;If you created a specific user for this database, make sure it has full privileges: Or else just use the postgres user created automatically when installing postgresql
To create a new user and give access to the database rest-api (Optional). Use the following sql commands
CREATE USER myapp_user WITH ENCRYPTED PASSWORD 'myapp_password';
GRANT ALL PRIVILEGES ON DATABASE rest-api TO myapp_user;Replace myapp_user and myapp_password with the user and password you are using in postgresql
To set up the environment variables for the project:
Run the following command to copy .env.example to .env:
cp .env.example .envOpen the newly created .env file and update the values to match your project configuration.
POSTGRES_DB=your_database
POSTGRES_USER=your_username
POSTGRES_PASSWORD=your_secure_password
SPRING_DATASOURCE_URL=jdbc:postgresql://db:5432/your_database
SPRING_DATASOURCE_USERNAME=your_username
SPRING_DATASOURCE_PASSWORD=your_secure_password
Note: Replace the placeholder values (
your_database,your_username,your_secure_password) with your actual database configuration.
Once configured, your application will be able to connect to the database using the specified environment variables.
mvn spring-boot:runor use:
Run the following command to compile and package the application:
mvn clean packageRun the JAR file after a successful build:
java -jar target/rest-api-0.0.1-SNAPSHOT.jar| Method | Endpoint | Description |
|---|---|---|
| GET | /students |
Get all students |
| GET | /subjects |
Get all subjects |
You can test the API using Postman or Web Browser:
http://localhost:8080/students
http://localhost:8080/subjectsIf running in the foreground, press CTRL + C to stop it.
If running in the background (JAR mode), find the process ID and kill it:
ps aux | grep java
kill -9 <PID>If PostgreSQL fails to start because the port is occupied, find and kill the process:
sudo lsof -i :5432
sudo kill -9 <PID>Ensure PostgreSQL is running and accepting connections:
sudo systemctl start postgresqlCheck logs if there are errors:
sudo journalctl -u postgresql --no-pager | tail -n 20This project is also deployed on a free-tier AWS Ubuntu server instance with Nginx and API endpoints are publicly accessible through
http:/16.16.224.53/students
http:/16.16.224.53/subjectsHow it works: Copies all data every time.
β
Pros: Fast restore, simple recovery.
β Cons: High storage, slow backups.
How it works: Backs up only changes since last backup (full or incremental).
β
Pros: Minimal storage, fast backups.
β Cons: Slow restore (depends on backup chain), fragile.
How it works: Backs up all changes since last full backup.
β
Pros: Faster restore than incremental, moderate storage.
β Cons: Slower backups over time.
| Type | Storage | Backup Speed | Restore Speed | Risk |
|---|---|---|---|---|
| Full | High | Slow | Fast | Low |
| Incremental | Low | Fast | Slow | High |
| Differential | Medium | Moderate | Moderate | Medium |
Best Use Cases:
- Full: Critical systems (e.g., databases).
- Incremental: Frequent backups (e.g., daily files).
- Differential: Balanced approach (e.g., weekly full + daily differential).
Tip: Hybrid strategies (e.g., weekly full + daily incremental) optimize efficiency.
| Script | Purpose | Frequency |
|---|---|---|
health_check.sh |
Monitor CPU/Memory/Disk + API health | Every 6 hours |
backup_api.sh |
Backup JAR + PostgreSQL DB | Daily at 2 AM |
update_server.sh |
Update OS + App + Restart Service | Every 3 days at 3 AM |
# 1. Install dependencies (if not installed in the created server instance)
sudo apt install -y curl postgresql-client git maven
# 2. Make scripts executable
chmod +x *.sh
# 3. Running the scripts (in the created server instance)
# Health Check (no sudo needed)
./health_check.sh
# Backup (requires sudo for DB access)
sudo ./backup_api.sh
# System Update (requires sudo)
sudo ./update_server.sh
# 4. Schedule (crontab -e)
0 */6 * * * /home/ubuntu/health_check.sh >> /var/log/server_health.log 2>&1
0 2 * * * /home/ubuntu/backup_api.sh >> /var/log/backup.log 2>&1
0 3 */3 * * /home/ubuntu/update_server.sh >> /var/log/update.log 2>&1-
Local: Follow Docker installation guide.
-
AWS EC2 Ubuntu Server instance:
sudo apt update && sudo apt install -y docker.io sudo systemctl start docker && sudo systemctl enable docker sudo usermod -aG docker ubuntu sudo curl -L "https://github.com/docker/compose/releases/download/v2.20.0/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose sudo chmod +x /usr/local/bin/docker-compose
docker build -t rest-api:latest .docker-compose up --builddocker pscurl http://localhost:8080/students
curl http://localhost:8080/subjectsSpring Boot API Logs
docker-compose logs apiShows logs for the rest-api-api-1 container (Spring Boot application).
PostgreSQL Database Logs
docker-compose logs dbShows logs for the rest-api-db-1 container (PostgreSQL database).
docker-compose stopdocker-compose downdocker-compose up -dPort conflicts commonly occur when a required port (e.g., for Spring Boot or PostgreSQL) is already in use by another process. This guide helps troubleshoot and resolve conflicts on ports 8080 and 5432.
Error starting userland proxy: listen tcp4 0.0.0.0:8080: bind: address already in usebind: address already in usefor port 5432
- Port
8080is typically used by Spring Boot. - Port
5432is the default for PostgreSQL. - A conflict arises when another service is already using these ports.
Use either of the following commands:
For Port 8080:
sudo netstat -tulnp | grep 8080
# or
sudo lsof -i :8080For Port 5432:
sudo netstat -tulnp | grep 5432
# or
sudo lsof -i :5432These commands return the process ID (PID) of the program using the port.
Once you have the PID from above:
sudo kill -9 <PID>Replace <PID> with the actual process ID.
If PostgreSQL is running as a system service and you want to disable it (e.g., to let Docker use port 5432):
sudo systemctl stop postgresql
sudo systemctl disable postgresqlOnce the port is free:
docker-compose up --buildIf stopping the conflicting service isnβt possible:
- Edit
docker-compose.yml - Modify the port mappings:
services:
app:
ports:
- "8081:8080" # Host:Container
db:
ports:
- "5433:5432"- Update your app configuration and tests to use the new ports (
8081and5433).
- Use unique ports when running multiple services locally.
- Add port checks to startup scripts.
- Consider using
.envfiles for configurable port management
Docker hub repository containing the docker image used in this application : https://hub.docker.com/r/mwemanoor/rest-api-api
If youβd like to contribute:
- Fork the repository.
- Create a new branch (
git checkout -b feature-branch). - Make your changes and commit (
git commit -m "Your Message"). - Push to your fork (
git push origin feature-branch). - Open a pull request.
This project is licensed under the MIT License.
Thanks to all contributors! If you find this project useful, Give it a star β on GitHub!