HAL 中的参数化链接 json
Parametarized links in HAL json
假设我有一个端点 GET /api/foos/{id}
,它有可选参数:includes、query、type 我应该为每个 'usecases' 创建一个 link 还是我可以将它作为一个单身 link?
它应该看起来更像这样:
"_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 },
}
如果我也有与分页相关的 links,比如 next、prev 等,我应该为它们也包含这些模板吗?例如:
"next": { "href": "/api/foos?page=2{?includes}", "templated": true }
根据RFC6570, Section 3.2.1(这是URL模板的基础)你可以添加多个参数,没有值的参数将被忽略:
A variable that is undefined (Section 2.3) has no value and is ignored by the expansion process.
这意味着对于您的示例,您可以使用以下 HAL 响应:
"_links":{
"self": { "href": "/api/foos/1" },
"query": { "href": "/api/foos/1{?includes}{?query}{?type}", "templated": true },
}
它也应该适用于您的分页示例。
假设我有一个端点 GET /api/foos/{id}
,它有可选参数:includes、query、type 我应该为每个 'usecases' 创建一个 link 还是我可以将它作为一个单身 link?
它应该看起来更像这样:
"_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 },
}
如果我也有与分页相关的 links,比如 next、prev 等,我应该为它们也包含这些模板吗?例如:
"next": { "href": "/api/foos?page=2{?includes}", "templated": true }
根据RFC6570, Section 3.2.1(这是URL模板的基础)你可以添加多个参数,没有值的参数将被忽略:
A variable that is undefined (Section 2.3) has no value and is ignored by the expansion process.
这意味着对于您的示例,您可以使用以下 HAL 响应:
"_links":{
"self": { "href": "/api/foos/1" },
"query": { "href": "/api/foos/1{?includes}{?query}{?type}", "templated": true },
}
它也应该适用于您的分页示例。