Skip to content

An API to product management. It built with Java, Spring Framework and Spring Boot. To create, update, delete ou retrieve products it uses H2 database (in memory)

leandrocezar/products-java-api

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

products-java-api

Build Status GitHub forks GitHub release (latest by date) GitHub language count GitHub code size in bytes GitHub repo size GitHub last commit

About the API

An API for product management. This project is built with Java, Spring Boot, and Spring Framework. The API main URL /products.

Features

This API provides HTTP endpoint's and tools for the following:

  • Create a product: POST/products
  • Update a product: PUT/products/{id}
  • Delete a product (by id): DELETE/products/{id}
  • Find a unique product by id: GET/products/{id}
  • Get all products: GET/products
  • Get product by name or description, min price and max price products: GET/products/search?q={expression}&min_price={minprice}&max_price={maxprice}

To test the application import src/main/resources/insomnia_collection.json file on Insomnia App

Details

POST/products

This end-point is called to add a new Product.

Body:

{
  "name": "Product name",
  "description": "Product description",
  "price": 99.5
}

Where:

name - product name (required)

description - product description (required)

price – product price(parsable as a BigDecimal (required)

Return: Returns all info about the added product including the generated id:

{
  "id": "2018795b-3537-4ddc-a22f-69e90116c6c4",
  "name": "Product name",
  "description": "Product description",
  "price": 99.5
}
  • 201 - Created: Everything worked as expected.
  • 400 - Bad Request: the request was unacceptable. Reason: missing a required parameter.
  • 500- Server Error: something went wrong on API.

PUT/products/{id}

This end-point is called to update a existing Product.

Path param:

id - product id to update (required)

Body:

{
  "name": "Product name",
  "description": "Product description",
  "price": 99.5
}

Where:

name - product name (required)

description - product description (required)

price – product price(parsable as a BigDecimal (required)

Return: Returns all info about the updated product including the generated id:

{
  "id": "2018795b-3537-4ddc-a22f-69e90116c6c4",
  "name": "Product name",
  "description": "Product description",
  "price": 99.5
}
  • 200 - OK: Everything worked as expected.
  • 400 - Bad Request: the request was unacceptable. Reason: missing a required parameter.
  • 404 - Not Found: The product with path param id not exists.
  • 500- Server Error: something went wrong on API.

DELETE/products/{id}

This end-point is called to delete a existing Product.

Path param:

id - product id to delete (required)

Return: Returns the http status code to operation:

  • 200 - OK: Everything worked as expected.
  • 404 - Not Found: The product with path param id not exists.
  • 500- Server Error: something went wrong on API.

GET/products/{id}

This end-point is called to find a unique product Product.

Path param:

id - product id to find (required)

Return: Returns all about the product:

{
  "id": "2018795b-3537-4ddc-a22f-69e90116c6c4",
  "name": "Product name",
  "description": "Product description",
  "price": 99.5
}
  • 200 - OK: Everything worked as expected.
  • 400 - Bad Request: the request was unacceptable. Reason: missing a required parameter.
  • 404 - Not Found: The product with path param id not exists.
  • 500- Server Error: something went wrong on API.

GET/products

This end-point is called to find all Products.

Return: Returns the list of products:

[
	{
	  "id": "2018795b-3537-4ddc-a22f-69e90116c6c4",
	  "name": "Product name",
	  "description": "Product description",
	  "price": 99.5
	},
	{
	  "id": "2018795b-3537-4ddc-a22f-69e901163364",
	  "name": "Product name 2",
	  "description": "Product description 2",
	  "price": 10.5
	},
]
  • 200 - OK: Everything worked as expected.
  • 500- Server Error: something went wrong on API.

GET/products/search?q={expression}&min_price={min_price}&max_price={max_price}

This end-point is called to find products by some cryteria.

Query params:

q - product name or description

min_price - Minimum product price

max_price - Maximum product price

Return: Returns the list of products:

[
	{
	  "id": "2018795b-3537-4ddc-a22f-69e90116c6c4",
	  "name": "Product name",
	  "description": "Product description",
	  "price": 99.5
	},
	{
	  "id": "2018795b-3537-4ddc-a22f-69e901163364",
	  "name": "Product name 2",
	  "description": "Product description 2",
	  "price": 10.5
	},
]
  • 200 - OK: Everything worked as expected.
  • 500- Server Error: something went wrong on API.

Technologies used

This project was developed with:

  • Java 8
  • Spring Boot 2.5.2
  • Maven
  • Log4j2
  • JUnit 5
  • H2
  • Swagger 3.0.0
  • Model Mapper 2.3.9

Compile and Package

The API also was developed to run with an jar. In order to generate this jar, you should run:

mvn package

This command will clean, compile and generate a jar at target directory, e.g. products-java-api-1.0.0-SNAPSHOT.jar

Execution

This project uses H2 database. This database run in memory!. On run the project automatically will create a database, table and populate with 5 products!.

Test

  • For unit test phase, you can run:
mvn test

Run

In order to run the API, run the jar as following:

java -jar products-java-api-1.0.0-SNAPSHOT.jar --spring.profiles.active=dev

or

mvn spring-boot:run -Dspring.profiles.active=dev

By default, the API will be available at http://localhost:9999

Documentation