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:
com.ecommerce
microcommerce
microcommerce
com.ecommerce.microcommerce
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
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.