0

我的数据模式看起来像

User很多 Transaction。两者都是数据库UserTransaction的不同表
我有REST端点

用户端点

GET /users/uuid # to get information about specific user
PUT /users/uuid # update information of a specific user  

事务端点

GET /transactions/uuid # get a specific transaction
GET /transactions/user_uuid # get all transactions of a specific user
GET /transactions/user_uuid/yyyy # get all transactions of a specific user in year yyyy
GET /transactions/user_uuid/yyyy/mm # get all transactions of a specific user in year yyyy, month mm
GET /transactions/user_uuid/yyyy/mm/dd # get all transactions of a specific user in year yyyy, month mm and day dd
  • 我的问题是,由于transactions将始终附加User资源,因此为它们设置不同的端点是否有意义?
  • 推荐什么来访问用户的交易?上面贴的那个?或类似的东西
GET /users/uuid/transactions # all transactions for a user
GET /users/uuid/transactions/yyyy # all transactions for a user for year yyyy
GET /users/uuid/transactions/yyyy/mm # all transactions for a user for year yyyy, month mm
GET /users/uuid/transactions/yyyy/mm/dd # all transactions for a user for year yyyy, month mm and day dd

我现在很困惑哪个更好。

谢谢

4

1 回答 1

1

如果您总是transactions在用户的上下文中访问,那么您的第二种方法对我来说是有意义的,因为正如您所说并且用户有很多transactions,使用您的第一种方法,url 设计可能会很混乱,因为不清楚日期部分是否引用transactions了用户资源。另一方面,如果您想transactions为所有用户阅读所有内容,那么您有两个端点是有意义的transactions

GET /transactions/ # reads all transactions for all users
GET /transactions/yyyy # reads all trasnsaction for all users with date 
GET /users/uuid/transactions # reads all transaction for a particular user
GET /users/uuid/transactions/yyyy # reads all transactions for a particular user with date        
于 2013-04-07T18:29:01.403 回答