简体   繁体   English

带资源和深度资源的REST API设计

[英]Rest API design with resource and deep resource

While designing an API which will have a resource and a deep resource (/resource/{id}/deepResource), Is it a good design to have the deepResource as a parameter in resource path when there are numerous dynamic deepResources?在设计一个有资源和深度资源(/resource/{id}/deepResource)的API时,当动态deepResource很多时,把deepResource作为资源路径的参数是不是一个好的设计?

For example: A post request to create a new resource under a section of main resource例如:在主资源的一个部分下创建一个新资源的post请求

POST: /accounts/{id}/{section}

{section} can be any deep resource under account like "comment", "service request", "checkbook request" etc. {section} 可以是账户下的任何深层资源,如“评论”、“服务请求”、“支票簿请求”等。

The idea is {section} can grow as the application grows.这个想法是 {section} 可以随着应用程序的增长而增长。 So instead of having multiple endpoints for each deep resource like /accounts/{id}/comment因此,不要为每个深层资源设置多个端点,例如 /accounts/{id}/comment

/accounts/{id}/service /accounts/{id}/service

/accounts/{id}/checks /accounts/{id}/checks

how about having /accounts/{id}/{section}?拥有 /accounts/{id}/{section} 怎么样?

Logic on the backend is handled accordingly for each deep resource that gets added in future.后端的逻辑会针对将来添加的每个深层资源进行相应处理。

Appreciate your insights.欣赏你的见解。

how about having /accounts/{id}/{section}?拥有 /accounts/{id}/{section} 怎么样?

If that works for you, go right ahead.如果这对您有用,请继续。

REST doesn't care how you implement your back end. REST并不关心你如何实现你的后端。

REST also doesn't care how you construct your resource identifiers. REST 也不关心您如何构建资源标识符。 Whether you use a URI template with one parameter, or two parameters, or no parameter at all, no longer matters once you've copied the request-target into the HTTP request.无论您使用带有一个参数、两个参数还是根本没有参数的 URI 模板,一旦将请求目标复制到 HTTP 请求中就不再重要了。

In some API description languages (swagger/openapi, jsonapi, etc) you may find it difficult to describe the nuances of the different sections, especially in cases where "comment", "service", "checks" have different content-types, different schemas, and so on.在某些 API 描述语言(swagger/openapi、jsonapi 等)中,您可能会发现很难描述不同部分的细微差别,尤其是在“评论”、“服务”、“检查”具有不同内容类型、不同模式等。

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

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