简体   繁体   English

REST API URL 设计最佳实践

[英]REST API URL design Best practices

I am developing a REST API for a system which has a lot of setting parameters.我正在为具有大量设置参数的系统开发 REST API。 All these parameters should be able to change.所有这些参数都应该能够改变。 These parameters are grouped in categories and have unique names.这些参数按类别分组并具有唯一名称。 The parameters have different measuring units and they are of different types (boolean, number or object with several properties).参数有不同的测量单位,它们是不同的类型(布尔值、数字或具有多个属性的对象)。 For example I have parameters "flow" and "threshold" which belong to category "process" and one parameter "name" which belongs to category "general".例如,我有属于类别“过程”的参数“流”和“阈值”和属于类别“一般”的一个参数“名称”。 What is the usual way to define URLs?定义 URL 的常用方法是什么? Is it:是吗:

/process/flow

/process/threshold

/general/name 

Or is it better that each parameter has an unique number (id).或者每个参数都有一个唯一的数字(id)更好。 And without using the categories the URL can be simply defined as follows:在不使用类别的情况下,URL 可以简单地定义如下:

/param/{id}

REST API URL design Best practices REST API URL 设计最佳实践

REST doesn't care about URL design. REST 不关心 URL 设计。 So long as the identifiers that you use conform to the production rules described by RFC 3986 , you can use any spelling you like.只要您使用的标识符符合RFC 3986描述的产生式规则,您就可以使用您喜欢的任何拼写。

/general/name
/param/12345
/3eaa7b95-c46b-4290-9614-546dc17c1f2d

Those are all fine .这些都很好 From the perspective of a general purpose component (like a web browser, or a cache), the URI is opaque -- we never "parse" the URI to extract semantic information from it.从通用组件(如 Web 浏览器或缓存)的角度来看,URI 是不透明的——我们从不“解析”URI 以从中提取语义信息。

What that means is that the server can embed within the URI whatever information it needs to identify the resource - whether that be a human readable token, or a surrogate key, or a hash... it's all good.这意味着服务器可以在 URI 中嵌入识别资源所需的任何信息——无论是人类可读的令牌、代理键还是散列......都很好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM