API 设计文档
Product API
POST /api/v1/product/create Creates new product with name,price,stock
Parameters
| name |
type |
data type |
description |
| name |
required |
string |
Product name |
| price |
required |
number |
Product price |
| stock |
required |
number |
Number of products in stock |
Responses
| http code |
content-type |
response |
201 |
application/json |
{"code":"201", "name":"name", "message":"Product created"} |
409 |
application/json |
{"code":"409","message":"Product with the same name alread exists"} |
PUT /api/v1/product/update Update the product name and/or price
Parameters
| name |
type |
data type |
description |
| name |
required |
string |
Product name |
| price |
optional |
number |
New product price |
| stock |
optional |
number |
Updated product stock |
Responses
| http code |
content-type |
response |
200 |
application/json |
{"code":"200", "name":"name", "message":"Product updated"} |
404 |
application/json |
{"code":"404","message":"Product not found"} |
GET /api/v1/product/{name} Get product
Responses
| http code |
content-type |
response |
200 |
application/json |
{"code":"200", "product": {"name","price","stock"}, "message":"Product retrieved"} |
404 |
application/json |
{"code":"404","message":"Product not found"} |
GET /api/v1/products Get all the products
Responses
| http code |
content-type |
response |
200 |
application/json |
{"code":"200", "products": Array of <product object>, "message":"Products retrieved"} |
DELETE /api/v1/product/{name} Delete a product
Responses
| http code |
content-type |
response |
200 |
application/json |
{"code":"200", "name":"name", "message":"Product deleted"} |
404 |
application/json |
{"code":"404","message":"Product not found"} |
DELETE /api/v1/products Delete all products
Responses
| http code |
content-type |
response |
200 |
application/json |
{"code":"200", "message":"All products deleted"} |
Java 实现
In java we would have created a product class, with all the name/price/stock as its fields. Then instead of an array of Products, we would create a Product repository to store the Product objects.
Next we will create a Product service, similar to the functions in our expressjs implementation. The services will be in charge of the program logic.
Finally we create a ProductController as the entrypoint to our springboot application. The Controller will be in charge of parsing the request parameters and passing it to Product serivce. After all the processing is done, the Controller will get the results from the services and put them in a proper format before sending them back to the client.