Skip to content

Repository files navigation

Product API • java-version spring-boot-version

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.

Demo

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.html

app demo

Installation

Step 1: Setting up PostgreSQL

You can use Docker to run PostgreSQL and pgAdmin. You can use my postgres-pgadmin repository to run PostgreSQL and pgAdmin for an easy setup.

Step 2: Clone the repository

Clone the repository to your local machine.

git clone https://github.com/yasinozkancs/product-api.git

Step 3: Check Database Credentials

If 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
...

Step 4: Build the application

Build the application using Gradle Wrapper.

./gradlew build --refresh-dependencies

Step 5: Run the application

Finally run the application.

./gradlew bootRun

Usage

You 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.

GET /products

Returns a list of all products.

Request

curl http://localhost:8080/products

Response

[
  {
    "id": 1,
    "name": "Product 1",
    "description": "Description 1",
    "price": 10.99
  },
  {
    "id": 2,
    "name": "Product 2",
    "description": "Description 2",
    "price": 9.99
  }
]

POST /products

Creates a new product and returns the created product with an Id.

Request

curl -X POST http://localhost:8080/products -H "Content-Type: application/json" -d '{"name": "Product 1", "description": "Description 1", "price": 10.99}'

Response

{
  "id": 1,
  "name": "Product 1",
  "description": "Description 1",
  "price": 10.99
}

PUT /products/{id}

Updates an existing product and returns the updated product.

Request

curl -X PUT http://localhost:8080/products/1 -H "Content-Type: application/json" -d '{"name": "Updated Product", "description": "Updated Description", "price": 20.99}'

Response

{
  "id": 1,
  "name": "Updated Product",
  "description": "Updated Description",
  "price": 20.99
}

DELETE /products/{id}

Deletes an existing product and returns HTTP 200 OK.

Request

curl -X DELETE http://localhost:8080/products/1

Response

HTTP 200 OK

Documentation

You can access the API documentation at http://localhost:8080/api-docs.

Deployment

This API runs on embedded Tomcat server.

License

This API is licensed under the MIT License.

About

This is a REST API for managing Products. It can be used as a base model and can be extended per your needs. It is built using Spring-Boot and uses PostgreSQL as its database. The API has documentation generated by springdoc-openapi.

Topics

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages