REST 是适合我的应用程序的解决方案吗? -(一个有很多动作的系统)
Is REST a good solution for my application? - (a system with many actions)
我正在开始一个新项目,我想弄清楚我是否应该遵循 RESTful 方法。通过阅读互联网上的不同来源,我对真正的 RESTful 方法应该如何感到越来越困惑。有人说 REST 请求(即 URI 和 HTTP 请求的其余内容)不应该使用 HTTP 动词以外的其他内容来描述操作。其他一些人说,由于 HTTP 动词是有限的,因此 HTTP 请求 body 可以提供有关需要对 URI 描述的数据应用哪些操作的附加信息。 两者中哪一个是正确的?我将简要描述我正在尝试构建的软件以及我认为应该如何使用 REST 实现它:用户必须能够 upload/delete/edit 服务器上的数据。然后,他必须能够以作业的形式在服务器上执行 10 种不同的分析(analysis01-analysis10)。因此,用户将向服务器提交作业。作业完成后,用户将能够获得分析结果。所以这是我认为在 REST 中实现它的正确方法:
数据集:
POST /datasets/ - To create/upload a new dataset
GET /datasets/ - To get a list of all the available datasets.
GET /datasets/01 - To get info about dataset 01
DELETE /datasets/01 - To delete dataset 01
职位:
POST /analysis01_jobs/ - Submit a new job for analysis01
GET /analysis01_jobs/ - Get a list with all the submitted analysis01 jobs
GET /analysis10_jobs/ - Get a list with all the submitted analysis10 jobs
GET /analysis01_jobs/01 - Get the status of job '01' which performs analysis of type 01
DELETE /analysis10_jobs/01 - Delete/Cancel job '01' which performs analysis of type 10
如您所见,每种类型的分析都有不同的 REST URL 作业路径。我想改变这一点并将所有作业放在同一路径下:“/analysis_jobs/”并在 HTTP header 中指定我想要的分析类型。但这将暗示我希望服务器执行的操作类型。因此,这不再是 RESTful。 对还是错?
作业完成后会生成 "result" 资源。所以 REST API 将是这样的:
- There is no POST here because only the job that runs on the server can generate results.
GET /analysis01_results/ - To get a list of all the available results from analysis01 jobs
GET /analysis01_results/01 - To get the results of a job that performed analysis of type 01. The body of the HTTP response will also contain info about the "parent" job and data.
DELETE /analysis01_results/01 - To delete the above results.
是上面的设计吗RESTful?
提前致谢!
你的方向是正确的,但你仍然可以缓和它。
1-
Is REST a good solution for my application?
是的,你可以比较一下。REST vs SOAP on this question
2-
Use PUT for Creating or Adding a resource or Job instead of POST.
Resources are manipulated using a fixed set of four create, read, update, delete operations: PUT, GET, POST, and DELETE. PUT creates a new resource, which can be then deleted by using DELETE. GET retrieves the current state of a resource in some representation. POST transfers a new state onto a resource. See Responding to HTTP Methods and Requests for more information.
来源 Oracle docs
REST 是关于机器对机器的通信。如果您没有至少 2 个不同的客户端,我认为不值得为实施 REST 服务付出努力。
Some people say that the REST request (ie, both the URI and the rest of the content of the HTTP request) should never describe an action with something other than the HTTP verbs. Some others say that since the HTTP verbs are limited, the HTTP request body can provide additional info regarding which action need to be applied on the data described by the URI. Which of the two is correct?
你应该阅读 Fielding dissertation,它是唯一真正的知识来源。 :D
两者都可能是错误的。第二个更接近真相。如果您需要描述一个新的动作,您总是可以定义一个新的资源并将其与现有的 HTTP 动词一起使用。例如 ANALYZE /datasets/1
可以用 POST /analysis/ {dataset: {id: 1}}
.
来描述
请注意,我们在这里谈论的是 hyperlinks:
POST /datasets/ - To create/upload a new dataset
这是一个动作,但在 /datasets/
的表示中也是一个 hyperlink,您可以使用 GET /datasets/
访问它。您可以添加到正文的是 To create/upload a new dataset
部分,因此客户端无需了解 URI 结构的任何信息即可理解 link 的作用。
{
href: "/datasets/",
method: "POST",
rel: "/docs/datasets/create"
}
您可以使用 GET /docs/datasets/create
访问表单描述。 (目前人们正在研究一种标准格式,以描述这些描述,例如 RDF + Hydra。但目前尚未准备好生产。)
当然。如果需要,您可以扩展描述:
{
href: "/datasets/",
method: "POST",
rel: "/docs/datasets/create",
label: "Create a new dataset",
description: "You can create a new dataset by sending this form.",
fields: {
label: {
type: "string",
label: "Title",
description: "You can give a title to the dataset here.",
...
},
records: {
type: "array",
...
}
}
}
但是如果您在文档文件中描述所有内容,则生成文档会更容易,对于 REST 客户端可能在 JSON-LD 中,对于客户端的第 3 方开发人员可能在 HTML 中.
POST /analysis01_jobs/ - Submit a new job for analysis01
GET /analysis01_results/ - To get a list of all the available results from analysis01 jobs
GET /analysis01_results/01 - To get the results of a job that performed analysis of type 01. The body of the HTTP response will also contain info about the "parent" job and data.
我宁愿使用
POST /jobs/ {analysis: {id: "01"}}
GET /analysis/01/jobs
GET /jobs/2345/result
我正在开始一个新项目,我想弄清楚我是否应该遵循 RESTful 方法。通过阅读互联网上的不同来源,我对真正的 RESTful 方法应该如何感到越来越困惑。有人说 REST 请求(即 URI 和 HTTP 请求的其余内容)不应该使用 HTTP 动词以外的其他内容来描述操作。其他一些人说,由于 HTTP 动词是有限的,因此 HTTP 请求 body 可以提供有关需要对 URI 描述的数据应用哪些操作的附加信息。 两者中哪一个是正确的?我将简要描述我正在尝试构建的软件以及我认为应该如何使用 REST 实现它:用户必须能够 upload/delete/edit 服务器上的数据。然后,他必须能够以作业的形式在服务器上执行 10 种不同的分析(analysis01-analysis10)。因此,用户将向服务器提交作业。作业完成后,用户将能够获得分析结果。所以这是我认为在 REST 中实现它的正确方法:
数据集:
POST /datasets/ - To create/upload a new dataset
GET /datasets/ - To get a list of all the available datasets.
GET /datasets/01 - To get info about dataset 01
DELETE /datasets/01 - To delete dataset 01
职位:
POST /analysis01_jobs/ - Submit a new job for analysis01
GET /analysis01_jobs/ - Get a list with all the submitted analysis01 jobs
GET /analysis10_jobs/ - Get a list with all the submitted analysis10 jobs
GET /analysis01_jobs/01 - Get the status of job '01' which performs analysis of type 01
DELETE /analysis10_jobs/01 - Delete/Cancel job '01' which performs analysis of type 10
如您所见,每种类型的分析都有不同的 REST URL 作业路径。我想改变这一点并将所有作业放在同一路径下:“/analysis_jobs/”并在 HTTP header 中指定我想要的分析类型。但这将暗示我希望服务器执行的操作类型。因此,这不再是 RESTful。 对还是错?
作业完成后会生成 "result" 资源。所以 REST API 将是这样的:
- There is no POST here because only the job that runs on the server can generate results.
GET /analysis01_results/ - To get a list of all the available results from analysis01 jobs
GET /analysis01_results/01 - To get the results of a job that performed analysis of type 01. The body of the HTTP response will also contain info about the "parent" job and data.
DELETE /analysis01_results/01 - To delete the above results.
是上面的设计吗RESTful?
提前致谢!
你的方向是正确的,但你仍然可以缓和它。
1-
Is REST a good solution for my application?
是的,你可以比较一下。REST vs SOAP on this question
2-
Use PUT for Creating or Adding a resource or Job instead of POST.
Resources are manipulated using a fixed set of four create, read, update, delete operations: PUT, GET, POST, and DELETE. PUT creates a new resource, which can be then deleted by using DELETE. GET retrieves the current state of a resource in some representation. POST transfers a new state onto a resource. See Responding to HTTP Methods and Requests for more information.
来源 Oracle docs
REST 是关于机器对机器的通信。如果您没有至少 2 个不同的客户端,我认为不值得为实施 REST 服务付出努力。
Some people say that the REST request (ie, both the URI and the rest of the content of the HTTP request) should never describe an action with something other than the HTTP verbs. Some others say that since the HTTP verbs are limited, the HTTP request body can provide additional info regarding which action need to be applied on the data described by the URI. Which of the two is correct?
你应该阅读 Fielding dissertation,它是唯一真正的知识来源。 :D
两者都可能是错误的。第二个更接近真相。如果您需要描述一个新的动作,您总是可以定义一个新的资源并将其与现有的 HTTP 动词一起使用。例如 ANALYZE /datasets/1
可以用 POST /analysis/ {dataset: {id: 1}}
.
请注意,我们在这里谈论的是 hyperlinks:
POST /datasets/ - To create/upload a new dataset
这是一个动作,但在 /datasets/
的表示中也是一个 hyperlink,您可以使用 GET /datasets/
访问它。您可以添加到正文的是 To create/upload a new dataset
部分,因此客户端无需了解 URI 结构的任何信息即可理解 link 的作用。
{
href: "/datasets/",
method: "POST",
rel: "/docs/datasets/create"
}
您可以使用 GET /docs/datasets/create
访问表单描述。 (目前人们正在研究一种标准格式,以描述这些描述,例如 RDF + Hydra。但目前尚未准备好生产。)
当然。如果需要,您可以扩展描述:
{
href: "/datasets/",
method: "POST",
rel: "/docs/datasets/create",
label: "Create a new dataset",
description: "You can create a new dataset by sending this form.",
fields: {
label: {
type: "string",
label: "Title",
description: "You can give a title to the dataset here.",
...
},
records: {
type: "array",
...
}
}
}
但是如果您在文档文件中描述所有内容,则生成文档会更容易,对于 REST 客户端可能在 JSON-LD 中,对于客户端的第 3 方开发人员可能在 HTML 中.
POST /analysis01_jobs/ - Submit a new job for analysis01
GET /analysis01_results/ - To get a list of all the available results from analysis01 jobs
GET /analysis01_results/01 - To get the results of a job that performed analysis of type 01. The body of the HTTP response will also contain info about the "parent" job and data.
我宁愿使用
POST /jobs/ {analysis: {id: "01"}}
GET /analysis/01/jobs
GET /jobs/2345/result