2026-08-21 14:40:05.708033+08 / restful-api

Resource Naming and URI Design

介绍资源命名、URI 路径、路径参数、查询参数和 HTTP Header 的 RESTful API 设计规范。

Restful ApiApi DesignOpenapi

Resource Naming and URI Design

静态路径段使用小写连字符分隔的单词

静态 URI 路径段必须使用小写单词,并使用连字符分隔。

示例:

http
/node-groups
/locked-cells

该规则只适用于静态路径段,不适用于路径参数名称。


路径参数可以出现在 URI 路径的任意段中

路径参数可以出现在 URI 末尾,也可以出现在其他路径段中。

示例:

http
/cells/{id}

/cells/{id}/adjacent-cells

/nodes/{id}

/nodes/{id}/connected-nodes

/node-groups/{id}

/node-groups/{id}/nodes

优先使用 id 作为主要身份属性

资源应使用唯一的 id 属性作为主要身份属性。

id 的值至少应在对象所属的资源集合范围内保持唯一。

示例:

请求:

http
GET /nodes/123 HTTP/1.1

响应:

http
HTTP/1.1 200 OK
Content-Type: application/json
json
{
  "id": "123",
  "userLabel": "KistaEast",
  "ipAddress": "192.168.1.1"
}

在这个示例中:

  • id 是主要标识符。
  • userLabel 和 ipAddress 是次要标识符。

当业务领域中的自然键能够保证唯一时,也可以使用自然键。


次要标识符可以使用矩阵参数

主要标识符通常应表示为路径参数:

http
/nodes/{id}/connected-nodes

次要标识符应带上属性名称,也可以使用矩阵参数表示。

主要标识符:

http
/nodes/{id}/connected-nodes

矩阵参数:

http
/nodes/userLabel={userLabel}/connected-nodes
http
/nodes/ipAddress={ipAddress}/connected-nodes

多个矩阵参数:

http
/nodes/ipAddress={ipAddress}/connected-nodes/cabinetName={cabinetName1},{cabinetName2};operationalState={operationalState}

矩阵参数允许将带名称的属性直接放入 URI 路径段中。


矩阵路径参数使用 camelCase

矩阵路径参数必须使用 camelCase。

不要使用:

  • snake_case
  • kebab-case

正确示例:

http
/nodes/userLabel={userLabel}/connected-nodes

/nodes/ipAddress={ipAddress}/connected-nodes

错误示例:

http
/nodes/user_label={user_label}

/nodes/ip-address={ip_address}

该规则不适用于表示主要标识符的简单路径参数。

示例:

http
/nodes/{id}

查询参数使用 camelCase

查询参数必须使用 camelCase。

不要使用:

  • snake_case
  • kebab-case

正确示例:

text
customerNumber

orderId

billingAddress

ipAddress

错误示例:

text
customer_number

order-id

billing_address

HTTP Header 字段优先使用连字符 Pascal Case

HTTP Header 字段应优先使用连字符 Pascal Case,以保持命名一致性。

像 ID 这样的常见缩写可以保留大写。

示例:

http
Accept-Encoding

Apply-To-Redirect-Ref

Disposition-Notification-Options

Original-Message-ID

根据 HTTP 标准,HTTP Header 名称不区分大小写。


资源名称使用复数

资源名称应使用复数形式。

大多数 REST API 暴露的是资源集合,即使某个资源当前只有一个实例,也应使用集合形式建模。

正确示例:

http
/users

/orders

/products

/devices

避免:

http
/user

/order

/product

/device

Singleton 资源仍然可以建模为元素数量为 1 的集合。


避免使用 /api 作为基础路径

API 基础路径通常不应包含 /api。

推荐:

http
/customers

/orders

/devices

避免:

http
/api/customers

/api/orders

/api/devices

部署基础路径应作为部署配置处理,并与资源路径分开定义。


避免尾部斜杠

资源 URI 不应以尾部斜杠结尾。

推荐:

http
/customers/123

避免:

http
/customers/123/

无论是否存在尾部斜杠,资源都应保持一致的行为;但客户端和服务端应统一使用不带尾部斜杠的格式。


标准查询参数

支持搜索、排序、过滤和分页的 API 应使用约定俗成的查询参数名称。

搜索

使用:

text
q

示例:

http
/products?q=phone

q 表示用户输入的搜索关键词。


排序

使用:

text
sort

参数值是以逗号分隔的字段列表。

示例:

http
/orders?sort=id

可以通过前缀指定排序方向:

升序:

http
/orders?sort=+id

降序:

http
/orders?sort=-createdAt

多个字段:

http
/orders?sort=-createdAt,status

部分响应字段

使用:

text
fields

示例:

http
/users?fields=id,name,email

这样客户端可以只请求所需的字段。


嵌入关联资源

使用:

text
embed

示例:

http
/orders?embed=customer

这样可以将关联资源直接嵌入响应中。


Offset 分页

使用:

text
offset

示例:

http
/orders?offset=100

offset 表示集合中第一个返回元素的位置。


Cursor 分页

使用:

text
cursor

示例:

http
/orders?cursor=eyJwYWdlIjoyfQ==

Cursor 应被视为不透明值。

客户端不得检查、修改或自行构造 Cursor 值。


限制分页大小

使用:

text
limit

示例:

http
/orders?limit=50

limit 允许客户端建议一次响应最多返回的资源数量。

Resource Naming and URI Design | Remi Resume