This is a REST API for managing products. The API handles basic CRUD operations for products. The API is built with Spring Boot and uses PostgreSQL database for storing the products.
In this API you will find examples of concepts such as dependency injection, exception handling, validation, business rules, request response pattern, mapping and so on. You can use this API as your backend service managing your products.
Below is a demonstration of the API in action using Swagger UI. You can access it at http://localhost:8080/swagger-ui/index.html once you start up the application.
firefox http://localhost:8080/swagger-ui/index.htmlYou can use Docker to run PostgreSQL and pgAdmin. You can use my postgres-pgadmin repository to run PostgreSQL and pgAdmin for an easy setup.
Clone the repository to your local machine.
git clone https://github.com/yasinozkancs/product-api.gitIf you haven't used my postgres-pgadmin repository as it is to
set up your
database, please check src/main/resources/application.properties file and make
sure you have the right username and password for your PostgreSQL instance.
Change directory to the project root and run the following command to check your database credentials.
cd product-api
cat src/main/resources/application.properties...
spring.datasource.username=user
spring.datasource.password=password
...
Build the application using Gradle Wrapper.
./gradlew build --refresh-dependenciesFinally run the application.
./gradlew bootRunYou can use the API with any Http Client capable of making required HTTP requests. You can use curl, postman, or swagger-ui as shown in the demo above to interact with the API.
Returns a list of all products.
curl http://localhost:8080/products
[
{
"id": 1,
"name": "Product 1",
"description": "Description 1",
"price": 10.99
},
{
"id": 2,
"name": "Product 2",
"description": "Description 2",
"price": 9.99
}
]Creates a new product and returns the created product with an Id.
curl -X POST http://localhost:8080/products -H "Content-Type: application/json" -d '{"name": "Product 1", "description": "Description 1", "price": 10.99}'{
"id": 1,
"name": "Product 1",
"description": "Description 1",
"price": 10.99
}Updates an existing product and returns the updated product.
curl -X PUT http://localhost:8080/products/1 -H "Content-Type: application/json" -d '{"name": "Updated Product", "description": "Updated Description", "price": 20.99}'{
"id": 1,
"name": "Updated Product",
"description": "Updated Description",
"price": 20.99
}Deletes an existing product and returns HTTP 200 OK.
curl -X DELETE http://localhost:8080/products/1HTTP 200 OK
You can access the API documentation at http://localhost:8080/api-docs.
This API runs on embedded Tomcat server.
This API is licensed under the MIT License.
