英文:
Expose single REST endpoint (GET) which returns a parent object and all the child objects associated with it
问题
以下是翻译好的部分:
- 实现一个名为 "parentWithchildren" 的 API(GET: /parentWithchildren/)
- 只有一个用于父对象的端点,它将返回一个带有一组子对象的父对象(GET: parent/)
英文:
Is it correct to implement one REST endpoint (GET) to expose parent and set of child objects together?
e.g:
> parent {
name: parent,
id: 123,
Children:{
{
childName:1
},
{
childName:2
}
}
}
I am thinking two approaches:
- Implement one API named "parentWithchildren" (GET: /parentWithchildren/)
- Just endpoint for parent, and it will return a Parent object with set of child objects (GET: parent/)
答案1
得分: 1
如果我们遵循 REST API 原则,并假设您的资源是 Parent,而且一个 Parent 可以拥有 children。假设每个 child 都可以通过 Id 进行标识,那么您的 REST 终端点将如下所示:
获取 /parent/{parentId}
获取 /parent/{parentId}/child/{childId}
现在,您可以创建一个粗糙版本的服务,如果您的 children 没有 ID,那么它将如下所示:
获取 /parent/{parentId}?fetchChildren=true
将终端点实现为 /parentWithchildren 更像是 RPC 风格的编程,不太符合 REST 风格。
英文:
If we follow the rest api principles and we assume that your resource is Parent and a Parent can have children. Lets say each child is identifiable by Id as well as the Parent then your rest endpoint will look like:
get /parent/{parentId}
get /parent/{parentId}/child/{childId}
Now you can potentialy create crude version of your service that is also useful if your children dont have ID than it will look like:
get /parent/{parentId}?fetchChildren=true
Implementing an endpoint as /parentWithchildren is more of a RPC style of programming, not very rest like.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论