Expose single REST endpoint (GET) which returns a parent object and all the child objects associated with it

huangapple go评论106阅读模式
英文:

Expose single REST endpoint (GET) which returns a parent object and all the child objects associated with it

问题

以下是翻译好的部分:

  1. 实现一个名为 "parentWithchildren" 的 API(GET: /parentWithchildren/)
  2. 只有一个用于父对象的端点,它将返回一个带有一组子对象的父对象(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:

  1. Implement one API named "parentWithchildren" (GET: /parentWithchildren/)
  2. 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.

huangapple
  • 本文由 发表于 2020年8月9日 20:20:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/63326281.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定