服务中的实体关系
Entity Relations in Services
我有这样的模型关系:User,Task,每个用户都可以创建自己的任务,可以看到所有 个任务。所以很明显我会有 TaskService 方法 getAllTasks(), getSortedTasks(String value), 等等.
我很困惑我应该在哪里使用 createTask(Task task), updateTask(lond id, Task task) 方法 - 在 TaskService 或 UserService,因为有人告诉我应该让 REST API 看起来像:
相当
post users/{id}/tasks
比
post /tasks
因为"a task belongs to a particular user"。所以我无法在每个实体的单独 services,api 和公共 services,api whre 用户 是关系所有者。
您应该实现用于在任务资源中创建任务的端点。
两者皆有可能:
POST your_root.com/api/{userId}/tasks/ ----> 创建任务并取值uri 的创建者。
但你也可以这样做:
POST your_root.com/api/tasks/ ---> 在这种情况下你必须通过 [= 告诉服务器45=] 或 XML 创建任务的人。 body 可能看起来像这样,即:
{
"author": "user A",
"assingee": null,
"headline": "Login Page for my app",
"text":"Build fancy login page"
}
最后一个变体和我的首选:
POST your_root.com/api/{userId}/tasks/
和 body 中的 authorId 再次:
{
"authorId":"12"
"author": "user A",
"assingee": null,
"headline": "Login Page for my app",
"text":"Build fancy login page"
}
最后一个变体的优点是您可以检查 uri 中的 userId 是否等于 body 的 authorId。这样就可以避免这个带有某个Id的请求是不小心发出的。
希望对您有所帮助。
我有这样的模型关系:User,Task,每个用户都可以创建自己的任务,可以看到所有 个任务。所以很明显我会有 TaskService 方法 getAllTasks(), getSortedTasks(String value), 等等. 我很困惑我应该在哪里使用 createTask(Task task), updateTask(lond id, Task task) 方法 - 在 TaskService 或 UserService,因为有人告诉我应该让 REST API 看起来像:
相当
post users/{id}/tasks
比
post /tasks
因为"a task belongs to a particular user"。所以我无法在每个实体的单独 services,api 和公共 services,api whre 用户 是关系所有者。
您应该实现用于在任务资源中创建任务的端点。
两者皆有可能:
POST your_root.com/api/{userId}/tasks/ ----> 创建任务并取值uri 的创建者。
但你也可以这样做:
POST your_root.com/api/tasks/ ---> 在这种情况下你必须通过 [= 告诉服务器45=] 或 XML 创建任务的人。 body 可能看起来像这样,即:
{
"author": "user A",
"assingee": null,
"headline": "Login Page for my app",
"text":"Build fancy login page"
}
最后一个变体和我的首选:
POST your_root.com/api/{userId}/tasks/ 和 body 中的 authorId 再次:
{
"authorId":"12"
"author": "user A",
"assingee": null,
"headline": "Login Page for my app",
"text":"Build fancy login page"
}
最后一个变体的优点是您可以检查 uri 中的 userId 是否等于 body 的 authorId。这样就可以避免这个带有某个Id的请求是不小心发出的。
希望对您有所帮助。