Get /users/2 Response 404
Represents the User whose request ID is 2, and returns 404 to represent that the resource was not found.
https://www.zhihu.com/question/28570307 The top-voted answer also solves some problems involved in designing rest APIs, such as the difficulty of denominalizing certain verbs, which involves business abstraction and part-of-speech conversion. In addition, for example, there are two parts of speech such as search. In fact, it can be used as part of the URL when it is designed. This is already reflected in the github API:
https://api.github.com/ "repository_search_url": "https://api.github.com/search/repositories?q={query}{&page,per_page,sort,order}",
Similar to login, although it is mentioned that it is operating a session, in practical applications, it is generally not handled by rest. If /login is used as the endpoint, everyone does not think that there is a big problem.
More complex problems are encountered in business systems, including
- 1. One-to-many, many-to-many rest API design, including create, update
- 2. The circular serialization problem of two-way binding.
Here first record the handling of the problem I have seen for 1.
Generally speaking, this kind of association is divided into two cases:
[list]
GET /persons/{PERSON_ID}/edu-histories
Get a single productGET /products/{product_id}
Get all products of an order GET /orders/{order_id}/productsGet
all orders associated with a productGET /products/{product_id }/orders
to create this kind of association can be achieved by POST/PUT/PATCH, just add the associated entity in the body. The specific implementation is also discussed in stackoverflow which method should be more in line with the specification. Everyone mentions PATCH. In practical applications, you can adjust whether your web server supports PATCH or not.
In this case, the entity should be returned separately as far as possible, and the associated entity should not be returned, such as getting the order, and the associated product will not be returned directly. A defect caused by this is that it needs to be called multiple times by the client to return all the results.
[/list]
This defect is actually the third layer of the original design concept of RestAPI - mentioned by hateoas, that is, the result sent back by each restAPI may not only be the current one, it should also include other related The link referenced by the object is just like when we visit a URL, the URL will not only return us the content, but also provide us with other related hyperlinks. Only after Hateoas can be considered truly Restful.
Another implementation is to treat this relationship as a class of resources, and the URL is directly represented by the relationship:
GET /order-product-relationship [{"orderId":1,"productId":2},{"orderId":1,"productId":3}]
to be continued