我是REST和Web API的新手。我對如何爲我的資源設計URI感到困惑。
鑑於我有一個域具有以下資源:博客,帖子和用戶。
Blog (1) ------ (0..*) Post (0..*) ------ (1) User
一個博客可以有很多職位,每個職位與一個博客有關。用戶可以有很多帖子,每個帖子都與一個用戶相關聯。
對於博客和用戶資源的URI是這樣的:
GET /blogs - get list of all blogs
GET /blogs/{id} - get blog by id
POST /blogs - create new blog
PUT /blogs/{id} - update blog
DELETE /blogs/{id} - delete blog
GET /users- get list of all users
GET /users/{id} - get user by id
POST /users - create new user
PUT /users/{id} - update user
DELETE /users/{id} - delete user
但對於帖子資源?如何處理關聯?我正在考慮以下替代方案 - 哪些是正確的,爲什麼?
- 通過博客
1. GET /blogs/{id}/posts
or
2. GET /posts?blogid={id}
獲取所有職位 - 在博客中
3. POST /blogs/{id}/posts
or
4. POST /posts (here I would then in the payload send the IDs of the resources this post is associated with. BlogId and UserId)
創建新的崗位 - 通過博客,並通過用戶獲得的所有帖子
5. GET /blogs/{id}/posts?userid={id}
or
6. GET /posts?blogid={id}&userid={id}
如果有人抱怨d在這裏指向正確的方向,我會很感激。
如果您想進行一些進一步閱讀的REST的API中的優點和細節,採取這裏也看看http://roysvork.wordpress.com/2013/03/13/why-im-giving-rest-a-rest/ – 2013-05-01 11:26:43