如何为相互依赖的资源设计RESTAPI?
How to design a REST API for resources that depend on each other?
我在为以下情况设计休息 api 时遇到问题:
- 当新用户注册时he/she也会创建一个新组。
- 新用户也可以使用唯一的群组代码加入现有群组。
- 现有用户可以创建新组。
- 现有用户可以加入现有群组。
我正在考虑按如下方式进行:
POST https://api.myapp.com/users(创建新用户和新组,将用户添加到组)
PATCH https://api.myapp.com/groups/{id}(创建新用户,将用户添加到现有组)
我不喜欢通过 /groups 端点创建新用户。
POST https://api.myapp.com/groups/{id}(创建新组,将用户添加到组)
PATCH https://api.myapp.com/groups/{id}(将用户添加到现有组)
我不知道如何以及是否应该在 api 中表达用户和组相互依赖。
类似于:
/用户/{id}/组
/组/{id}/用户
如果这是更好的方法,那么群体或用户应该首先考虑什么?
将用户添加到组时,组应该在 URL 中排在第一位。
群组资源:
{
"id": "group1",
"name": "Developers",
"users": [
{
"id": "user1",
"name": "John1"
},
{
"id": "user2",
"name": "John2"
}
]
}
用户资源:
{
"id": "user1",
"groups": "John1",
"users": [
{
"id": "group1",
"name": "Developers"
},
{
"id": "group2",
"name": "Testers"
}
]
}
创建新群组:POST https://api.myapp.com/groups
Request URL: https://api.myapp.com/groups
Request Body:
{
"name": "Developers",
}
Response Body:
{
"id": "group1",
"name": "Developers"
}
HTTP Status Code: 201
创建一个新用户并将其添加到现有组:POST https://api.myapp.com/groups/{groupId}/users
Request URL: https://api.myapp.com/groups/{group1}/users
Request Body:
{
"name": "John3"
}
Response Body:
{
"id": "user3",
"name": "John3",
"groups": [
{
"id": "group1",
"name": "Developers"
}
]
}
HTTP Status Code: 201
创建新用户和组:POST https://api.myapp.com/users
Request URL: https://api.myapp.com/users
Request Body:
{
"name": "John4",
"groups": [
{
"name": "Testers"
}
]
}
Response Body:
{
"id": "user4",
"name": "John4",
"groups": [
{
"id": "group2",
"name": "Testers"
}
]
}
将现有用户添加到现有组:POST https://api.myapp.com/groups/{groupId}/users/{userId}
Request URL: https://api.myapp.com/groups/{group2}/users/{user1}
Request Body: No Body
Response Body: No Body
HTTP Status Code: 200
获取群:GET https://api.myapp.com/groups/{groupId}
Request URL: https://api.myapp.com/groups/{group1}
Response Body:
{
"id": "group1",
"name": "Developers",
"users": [
{
"id": "user1",
"name": "John1"
},
{
"id": "user2",
"name": "John2"
},
{
"id": "user3",
"name": "John3"
}
]
}
获取用户:GET https://api.myapp.com/users/{userId}
Request URL: https://api.myapp.com/users/{user1}
Response Body:
{
"id": "user1",
"name": "John1",
"groups": [
{
"id": "group1",
"name": "Developers"
},
{
"id": "group2",
"name": "Testers"
}
]
}
我在为以下情况设计休息 api 时遇到问题:
- 当新用户注册时he/she也会创建一个新组。
- 新用户也可以使用唯一的群组代码加入现有群组。
- 现有用户可以创建新组。
- 现有用户可以加入现有群组。
我正在考虑按如下方式进行:
POST https://api.myapp.com/users(创建新用户和新组,将用户添加到组)
PATCH https://api.myapp.com/groups/{id}(创建新用户,将用户添加到现有组) 我不喜欢通过 /groups 端点创建新用户。
POST https://api.myapp.com/groups/{id}(创建新组,将用户添加到组)
PATCH https://api.myapp.com/groups/{id}(将用户添加到现有组)
我不知道如何以及是否应该在 api 中表达用户和组相互依赖。 类似于:
/用户/{id}/组 /组/{id}/用户
如果这是更好的方法,那么群体或用户应该首先考虑什么?
将用户添加到组时,组应该在 URL 中排在第一位。
群组资源:
{
"id": "group1",
"name": "Developers",
"users": [
{
"id": "user1",
"name": "John1"
},
{
"id": "user2",
"name": "John2"
}
]
}
用户资源:
{
"id": "user1",
"groups": "John1",
"users": [
{
"id": "group1",
"name": "Developers"
},
{
"id": "group2",
"name": "Testers"
}
]
}
创建新群组:POST https://api.myapp.com/groups
Request URL: https://api.myapp.com/groups Request Body: { "name": "Developers", } Response Body: { "id": "group1", "name": "Developers" } HTTP Status Code: 201
创建一个新用户并将其添加到现有组:POST https://api.myapp.com/groups/{groupId}/users
Request URL: https://api.myapp.com/groups/{group1}/users Request Body: { "name": "John3" } Response Body: { "id": "user3", "name": "John3", "groups": [ { "id": "group1", "name": "Developers" } ] } HTTP Status Code: 201
创建新用户和组:POST https://api.myapp.com/users
Request URL: https://api.myapp.com/users Request Body: { "name": "John4", "groups": [ { "name": "Testers" } ] } Response Body: { "id": "user4", "name": "John4", "groups": [ { "id": "group2", "name": "Testers" } ] }
将现有用户添加到现有组:POST https://api.myapp.com/groups/{groupId}/users/{userId}
Request URL: https://api.myapp.com/groups/{group2}/users/{user1} Request Body: No Body Response Body: No Body HTTP Status Code: 200
获取群:GET https://api.myapp.com/groups/{groupId}
Request URL: https://api.myapp.com/groups/{group1} Response Body: { "id": "group1", "name": "Developers", "users": [ { "id": "user1", "name": "John1" }, { "id": "user2", "name": "John2" }, { "id": "user3", "name": "John3" } ] }
获取用户:GET https://api.myapp.com/users/{userId}
Request URL: https://api.myapp.com/users/{user1} Response Body: { "id": "user1", "name": "John1", "groups": [ { "id": "group1", "name": "Developers" }, { "id": "group2", "name": "Testers" } ] }