A RESTful API built with Spring Boot for managing products, using PostgreSQL as the database and Docker for containerization.
- ✅ Full CRUD operations for Product entities
- ✅ PostgreSQL database integration
- ✅ Docker Compose setup for easy deployment
- ✅ Spring Data JPA for database operations
- ✅ RESTful API design
- Java: 25
- Spring Boot: 3.5.7
- Spring Data JPA: For database operations
- PostgreSQL: Database
- Docker: Containerization
- Maven: Build tool
apirest/
├── src/
│ ├── main/
│ │ ├── java/com/apirest/apirest/
│ │ │ ├── ApirestApplication.java
│ │ │ ├── Controllers/
│ │ │ │ └── ProductController.java
│ │ │ ├── Entities/
│ │ │ │ └── Product.java
│ │ │ └── Repositories/
│ │ │ └── ProductRepository.java
│ │ └── resources/
│ │ └── application.properties
│ └── test/
├── docker-compose.yml
├── .env.template
└── pom.xml
- Java 25 or higher
- Maven 3.x
- Docker and Docker Compose
-
Clone the repository
git clone git@github.com:irenemonzon/java-crud-api-rest.git
-
Configure environment variables
Copy the
.env.templatefile to.env:cp .env.template .env
Edit the
.envfile with your database credentials:SPRING_DATASOURCE_URL=jdbc:postgresql://localhost:5432/productdb SPRING_DATASOURCE_USERNAME=your_username SPRING_DATASOURCE_PASSWORD=your_password SPRING_DATASOURCE_DB=productdb
-
Start PostgreSQL with Docker
docker compose up -d
The API will be available at http://localhost:8080
http://localhost:8080/products
| Method | Endpoint | Description |
|---|---|---|
| GET | /products |
Get all products |
| GET | /products/{id} |
Get product by ID |
| POST | /products |
Create a new product |
| PUT | /products/{id} |
Update a product |
| DELETE | /products/{id} |
Delete a product |
Create a Product
curl -X POST http://localhost:8080/products \
-H "Content-Type: application/json" \
-d '{
"name": "Laptop",
"price": 999.99
}'Get All Products
curl http://localhost:8080/productsGet Product by ID
curl http://localhost:8080/products/1Update a Product
curl -X PUT http://localhost:8080/products/1 \
-H "Content-Type: application/json" \
-d '{
"name": "Gaming Laptop",
"price": 1299.99
}'Delete a Product
curl -X DELETE http://localhost:8080/products/1| Column | Type | Constraints |
|---|---|---|
| id | BIGINT | PRIMARY KEY, AUTO_INCREMENT |
| name | VARCHAR | - |
| price | DOUBLE | - |
The application configuration is managed through application.properties:
- Server Port: 8080
- JPA: Auto-update schema
- Database: PostgreSQL (configured via environment variables)
- Ensure PostgreSQL is running:
docker-compose ps - Verify environment variables in
.envfile - Check if port 5432 is available
If port 8080 is already in use, modify server.port in application.properties