树形层次结构的REST URL架构应如何显示? [英] How should a REST URL schema look like for a tree hierarchy?

查看:157
本文介绍了树形层次结构的REST URL架构应如何显示?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们假设我有商店,商店中的货架以及货架上的产品.因此,为了获取商店货架上的产品列表,我将使用以下请求:

Let's assume that I have stores, shelves in a store, and products on a shelf. So in order to get a list of products on a shelf in a store, I'd use the following request:

GET http://server/stores/123/shelves/456/products

从这里,我将如何获得单个产品?我应该使用:

From here, how would I get an individual product? Should I use:

GET http://server/products/789

或者:

GET http://server/stores/123/shelves/456/products/789

第一种方法更为简洁,因为一旦您获得了产品列表,您只想查看特定产品的详细信息就不必在乎它属于哪个商店.但是,第二种方法更具逻辑性,因为您正在查看特定商店中特定货架的产品.

The first method is more concise, since once you get a list of products, you don't really care which store it belongs to if you just want to view the details for a particular product. However, the second method is more logical, since you're viewing the products for a specific shelf in a specific store.

同样,PUT/DELETE操作又如何呢?

Likewise, what about a PUT/DELETE operation?

DELETE http://server/stores/123/shelves/456/products/789

或者:

DELETE http://server/products/789

为这样的树层次结构设计架构的正确方法是什么?

What would be the correct way of designing a schema for a tree hierarchy like this?

P.S.如果我对REST架构有误解,请提供一些示例,说明如何将其做得更好.有太多人喜欢说"REST不是CRUD"和"REST不是RPC",然后绝对不提供任何说明或良好RESTful设计的示例.

P.S. If I'm misunderstanding something about the REST architecture, please provide examples on how I can make this better. There's way too many people who love to say "REST is not CRUD" and "REST is not RPC", then provide absolutely no clarifications or examples of good RESTful design.

推荐答案

不要基于URL结构设计REST api. 此处我认为您应该如何设计REST api.

Don't design a REST api based on an URL structure. Here is how I think you should go about designing a REST api.

尝试定义REST接口而不讨论讨论哪些资源中将包含哪些链接,就像讨论RPC接口并忽略参数和返回值一样.

Trying to define a REST interface without discussing what links will be contained in what resources is like discussing an RPC interface and ignoring parameters and return values.

这篇关于树形层次结构的REST URL架构应如何显示?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆