我正在通过为'问题跟踪应用程序'创建RESTful服务来学习REST架构。很多人都知道ITA有用户、项目、问题和评论。
关系如下:
- 项目有问题,这些问题也由某个用户管理。
- 问题有评论,这些评论反过来也由某个用户管理(让我们说发布)
- 项目也由用户管理。
我被安排路线的决定卡住了。我在一个文本文件中写了一些路由,我在这里发布。请大家看一看,提出改进意见,找出错误。
Models:
- User
- Project
- Issue
- Comment
1. User
> Immediate Requirement
GET api/users/ /* Authenticate, and then return list of all users */
GET api/users/:id /* Authenticate, and return a particular user */
POST api/users/ /* Create a new user */
PUT api/users/:id /* Authenticate, and update a particular user */
> Add-on Requirement
GET api/users/:id/projects/ /* Authenticate, and return list of projects of particular user */
> Never Mind Requirement
DELETE api/users/ /* Authenticate, and delete all users */
DELETE api/users/:id /* Authenticate, and delete a particular user */
2. Project
> Immediate Requirement
GET api/projects/ /* Authenticate, and then return list of all projects */
GET api/projects/:id /* Authenticate, and return a particular project */
POST api/projects/ /* Authenticate, and create a new project */
PUT api/projects/:id /* Authenticate, and update a particular project */
> Add-on Requirement
GET api/projects/:id/issues /* Authenticate, and return a list of issues of particular project */
POST api/projects/:id/issues /* Authenticate, and create new issue for particular project */
DELETE api/projects/:id/issues /* Authenticate, and delete all issues of particular project */
> Never Mind Requirement
DELETE api/projects/ /* Authenticate, and delete all projects */
DELETE api/projects/:id /* Authenticate, and delete a particular project */
3. Issue
> Immediate Requirement
GET api/issues/ /* Authenticate, and then return list of all issues */
GET api/issues/:id /* Authenticate, and return a particular issue */
PUT api/issues/:id /* Authenticate, and update a particular issue */
> Add-on Requirement
GET api/issues/:id/comments /* Authenticate, and return a list of comments of particular issue */
POST api/issues/:id/comments /* Authenticate, and create a new comment for particular issue */
DELETE api/issues/:id/comments /* Authenticate, and delete all comments of particular issue */
> Never Mind Requirement
DELETE api/issues/ /* Authenticate, and delete all issues */
DELETE api/issues/:id /* Authenticate, and delete a particular issue */
4. Comment
> Immediate Requirement
GET api/comments/ /* Authenticate, and then return list of all comments */
GET api/comments/:id /* Authenticate, and return a particular comment */
PUT api/comments/:id /* Authenticate, and update a particular comment */
> Never Mind Requirement
DELETE api/comments/ /* Authenticate, and delete all comments */
DELETE api/comments/:id /* Authenticate, and delete a particular comment */
这篇文章将帮助很多试图学习REST的用户,因为关系是这个架构中最重要也是最困难的东西。
我想评论一下DELETE动词。对所有记录使用PUT和DELETE是一种不好的做法。
您已经在少数位置的所有实例上使用了DELETE。这可能是您的要求。在这种情况下,没有其他选择,但如果这不是您的绝对需求,那么最好遵循最佳实践。
REST对URI设计没有限制。例如下面的URI api/issues/:id/comments
,唯一重要的部分是:id
。其他任何事情都可能是基于意见的。这是因为您通过REST发送回链接,链接将包含URI或URI模板,因此您的客户端不需要从头构建URI。这些链接具有链接关系,客户端将使用它来检查链接的功能以及如何显示它。因此,URI仅供内部使用,如果遵循HATEOAS约束,则不需要编制文档。另一个参考:http://roy.gbiv.com/untangled/2008/rest-apis-must-be-hypertext-driven
如果你问我,我更喜欢扁平的URI结构,而不是分层的:api/comments/?issue=:id
和我用/
在集合URI的末尾,但这只是我个人的品味。