I'm developing a REST service, and I'm trying to adhere to Doctor Roy Fielding's conventions and guidelines.
I picture my service as an endpoint that exposes a set of resources. A resource is identified by an URI, and api clients can manipulate resources by using using HTTP semantics (i.e., different HTTP verbs map to the corresponding operations over URIs).
Guidelines state that these URIs should be defined in an hierarchical way, reflecting object hierarchy. This renders useful in resource creating, because on the backend we need the data to perform the create operation. However, on further manipulations, a lot of the information included in the URI is not even going to be used by the service, because generally the resource Id alone is enough to uniquely identify the operation target.
An example: Consider an Api that exposes the creation and management of products. Consider also that a product is associated with a brand. On creation it makes sense that the following action is performed:
HTTP POST /Brand/{brand_id}/Product
[Body containing the input necessary to the creation of the product]
The creation returns an HTTP 201 created with a location header that exposes the newly created product's location.
On further manipulations, clients could access the product by doing:
HTTP PUT /Brand/{brand_id}/Product/{product_id}
HTTP DELETE /Brand/{brand_id}/Product/{product_id}
etc
However, since the product Id is universal in the product scope, the following manipulations could be performed like this:
/Product/{product_id}
I am only keeping the /Brand/{brand_id} prefix for coherence reasons. In fact, the brand id is being ignored by the service. Do you thing that this is a good practice, and is reasonable for the sake of maintaining a clear, unambiguous ServiceInterface definition? What are the benefits of doing this, and is this the way to go at all?
Also any pointers on URI definition best practices would be appreciated.
Thanks in advance
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…