This project is a simple RESTful API for managing products. It allows users to perform basic CRUD (Create, Read, Update, Delete) operations on products.
To set up this project locally, you can use the Spring Initializr to generate the project structure with the necessary dependencies.
The following dependencies are included in the project:
- Spring Web: For building web applications, including RESTful services.
- Spring DevTools: For enhancing the development experience with automatic restarts and live reload.
- Go to Spring Initializr.
- Fill in the details as follows:
- Project: Maven Project
- Language: Java
- Spring Boot: 3.3.4
- Packaging: Jar
- Java: 17
- Group:
com.ecommerce
- Artifact:
microcommerce
- Name:
microcommerce
- Description: A RESTful API for managing products with basic CRUD operations. Built for microservices architecture.
- Package Name:
com.ecommerce.microcommerce
- Add the required dependencies:
- Spring Web
- Spring DevTools
- Click on the Generate button to download the project as a ZIP file.
- Extract the ZIP file and open it in your favorite IDE (e.g., IntelliJ IDEA, Eclipse).
- Build and run the application.
- List all products
- Retrieve a specific product by ID
- Add a new product
- Update an existing product by ID
- Remove a product by ID
curl -X GET http://localhost:8080/products | jq
curl -X GET http://localhost:8080/products/1 | jq
curl -X POST http://localhost:8080/products -H "Content-Type: application/json" -d '{"id": 21, "name": "New Product", "price": 500}' | jq
curl -X PUT http://localhost:8080/products/1 -H "Content-Type: application/json" -d '{"id": 1, "name": "Updated Product", "price": 1600}' | jq
curl -X DELETE http://localhost:8080/products/1 | jq
To create a JAR file for the project, ensure you have Maven installed. Then, navigate to the root of your project directory and run the following command:
cd microcommerce
mvn clean package -DskipTests
After executing this command, the JAR file will be generated in the target
directory, typically named microcommerce-0.0.1-SNAPSHOT.jar
.
To create a Docker image for your Spring Boot application, you need a Dockerfile
. In the root of your project directory, create a file named Dockerfile
with the following content:
# Use a base image that includes Java
FROM openjdk:17-jdk-slim
# Set the working directory in the container
WORKDIR /app
# Copy the Maven build output (JAR file) to the container
COPY target/microcommerce-0.0.1-SNAPSHOT.jar microcommerce.jar
# Expose the application port
EXPOSE 8080
# Define the command to run the application
ENTRYPOINT ["java", "-jar", "microcommerce.jar"]
Then, build the Docker image using the following command:
docker build -t microcommerce-image .
Once the Docker image is built, you can create and run a Docker container using the following command:
docker run -d -p 8080:8080 --name microcommerce-container microcommerce-image
- This command will run the container in detached mode, mapping port 8080 on your host to port 8080 in the container.
After the container is running, you can access the Spring Boot application at:
http://localhost:8080/products
To stop the running container, use the following command:
docker stop microcommerce-container
To remove the container, use:
docker rm microcommerce-container
Contributions are welcome! Please feel free to submit a pull request or open an issue.
This project is licensed under the MIT License - see the LICENSE file for details.