1

假设我有一个GET /api/foos/{id}具有可选参数的端点:包含、查询、类型我应该为每个“用例”创建一个链接还是可以将其作为单个链接包含在内?

它应该看起来更像这样:

"_links":{
   "self": { "href": "/api/foos/1" },
   "includes": { "href": "/api/foos/1{?includes}", "templated": true },
   "query": { "href": "/api/foos/1{?query}", "templated": true },
   "type": { "href": "/api/foos/1{?type}", "templated": true },
}

或者可能是这样的:

"_links":{
   "self": { "href": "/api/foos/1" },
   "query": { "href": "/api/foos/1{?includes}{?query}{?type}", "templated": true },
}

如果我也有与分页相关的链接,例如 next、prev 等,我是否也应该为它们包括这些模板?例如:

"next": { "href": "/api/foos?page=2{?includes}", "templated": true }
4

1 回答 1

1

根据RFC6570,第 3.2.1 节(这是 URL 模板的基础),您可以添加多个参数,并且没有值的参数将被忽略:

未定义的变量(第 2.3 节)没有任何值,并且会被扩展过程忽略。

这意味着对于您的示例,您可以使用以下 HAL 响应:

"_links":{
   "self": { "href": "/api/foos/1" },
   "query": { "href": "/api/foos/1{?includes}{?query}{?type}", "templated": true },
}

它也应该适用于您的分页示例。

于 2021-10-12T11:58:54.410 回答