A sample spring boot REST API app that enables CRUD operations on Products. Users can place Orders containing multiple such products. The price of products can be changed but already placed orders will still be able to show the old prices used during order placement. Spring JPA and H2 in memory DB are used for this app.
This code required the following to be built/packaged.
Maven 3.0+
Java 1.8
[H2 database and Tomcat included in the code with spring boot dependencies]
- Run the below maven command to install the app.This creates an executable jar file in /target folder.
mvn install
- To run the app using java command line, run the following command.
java -jar demo-0.0.1-SNAPSHOT.jar
http://localhost:8080/swagger-ui.html#/
http://localhost:8080/console/
Driver Class: org.h2.Driver
JDBC URL: jdbc:h2:mem:testdb
username: sa
password:
These endpoints allow for CRUD operation on the products repository.
This request creates a product and auto generates an id. The response will be HTTP 200 for success and a json containing the created product is returned
curl -X POST --header 'Content-Type: application/json' --header 'Accept: application/json' -d '{"name":"paper","price":1}' 'http://localhost:8080/products/'
{ "id": 1, "name": "paper", "price": 1 }
There are two options to read products.One endpoint to read a prticular product and another to list all available products.
curl -X GET --header 'Accept: application/json' 'http://localhost:8080/products/'
[ { "id": 1, "name": "paper", "price": 1.2 } ]
curl -X GET --header 'Accept: application/json' 'http://localhost:8080/products/1'
{ "id": 1, "name": "paper", "price": 1.2 }
This request updates an existing product. The response will be HTTP 200 for success and a json containing the updated product is returned In this example we are updating the price of the product with id 1
curl -X PUT --header 'Content-Type: application/json' --header 'Accept: application/json' -d '{"name":"paper","price":1.2}' 'http://localhost:8080/products/1'
{ "id": 1, "name": "paper", "price": 1.2 }
This request removes a product from the database. The response will be HTTP 200 for success.
curl -X DELETE --header 'Accept: /' 'http://localhost:8080/products/1'
200
These endpoints allow for creating,reading,deleting and searching through orders based on start and end dates.
This request creates an order and auto generates an id. The response will be HTTP 200 for success and a json containing the created product is returned. The main prerequisite is to already have the products available and then reference the product ids in the order JSON under the shoppingCartItemsList.
curl -X POST --header 'Content-Type: application/json' --header 'Accept: application/json' -d '{ "email":"abc%40def.com", "paymentSuccessFul":true, "shoppingCartItemsList":[ { "quantity":5, "product":{ "id":2 } },{ "quantity":2, "product":{ "id":3 } } ] }' 'http://localhost:8080/orders/'
{ "id": 2, "email": "[email protected]", "orderTotalPrice": 8, "paymentSuccessFul": true, "orderDate": "2018-12-06T16:15:59.313+0000", "shoppingCartItemsList": [ { "quantity": 5, "price": 1, "product": { "id": 2, "name": "PAPER", "price": 1 } }, { "quantity": 2, "price": 1.5, "product": { "id": 3, "name": "PEN", "price": 1.5 } } ] }
There are three options to read orders.
- Read all orders
- Read a particular order with order id
- Search for orders within a timeframe.
Let's see the search request here:
curl -X GET --header 'Accept: application/json' 'http://localhost:8080/orders/search?startdate=2018-12-06T14%3A38%3A22.236Z&enddate=2018-12-06T17%3A00%3A50.936Z'
[ { "id": 2, "email": "[email protected]", "orderTotalPrice": 8, "paymentSuccessFul": true, "orderDate": "2018-12-06T16:15:59.313+0000", "shoppingCartItemsList": [ { "quantity": 5, "price": 1, "product": { "id": 2, "name": "PAPER", "price": 1 } }, { "quantity": 2, "price": 1.5, "product": { "id": 3, "name": "PEN", "price": 1.5 } } ] } ]
This request removes the order from the database.
curl -X DELETE --header 'Accept: /' 'http://localhost:8080/orders/1'
200