Restful 响应中的 json 数据是否应该包含对象类型信息?

发布于 2024-12-26 01:59:10 字数 994 浏览 0 评论 0原文

假设我们想要填充一些 javascript 模型(例如backbone.js 模型),给定来自服务器的 json 响应,如下所示:

{
  "todo": {
    "title": "My todo",
    "items": [
      { "body": "first item." },
      { "body": "second item"}
    ]
  }
}

此数据不包含类型信息,因此当我们看到 < 时,我们不知道要填充哪个模型代码>“待办事项”键。

当然,可以创建一些自定义标准来将 json 响应对象中的键链接到客户端模型。例如:

{
  "todo": {
    "_type": "Todo",
    "title": "My todo",
    ...
  }
}

虽然这适用于对象,但当涉及到列表时就会变得尴尬:

"items": {
  "_type": "TodoItem",
  "_value": [
    { "body": "first item." },
    { "body": "second item"}
  ]
}

在创建此自定义规则之前,问题是:

  • 是否有关于在响应数据中包含客户端类型信息的 RESTful 指南?

  • 如果不是,在响应 json 中包含客户端类型信息是否是一个好主意?

  • 除了填充模型的整个方法之外,还有哪些其他替代方案?

编辑

虽然可以从 URL 检索模型类型,例如 /todo/user,但这种方法的问题是初始群体N 个模型意味着 N 个 http 请求。

相反,初始填充可以通过单个大合并树完成,只需 1 个请求。在这种情况下,url 中的模型类型信息会丢失。

Let's say we want to populate some javascript models (eg backbone.js models), given a json response from a server like this:

{
  "todo": {
    "title": "My todo",
    "items": [
      { "body": "first item." },
      { "body": "second item"}
    ]
  }
}

This data does not contain the type information, so we do not know which model to populate when we see the "todo" key.

Of course one can create some custom standard to link the keys in the json response object to the client side models. For instance:

{
  "todo": {
    "_type": "Todo",
    "title": "My todo",
    ...
  }
}

While this works for objects, it gets awkward when it comes to lists:

"items": {
  "_type": "TodoItem",
  "_value": [
    { "body": "first item." },
    { "body": "second item"}
  ]
}

Before creating this custom rules, the questions are:

  • Are there any RESTful guidelines on including client side type information in response data?

  • If not, is it a good idea to include the client side type information in the response json?

  • Beside this whole approach of populating models, what are other alternatives?

Edit

While the model type can be retrieved from the url, eg /todo and /user, the problem with this approach is that the initial population of N models would mean N http requests.

Instead, the initial population can be done from a single big merged tree with only 1 request. In this case, the model type information in the url is lost.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

﹏半生如梦愿梦如真 2025-01-02 01:59:11
  1. REST与来回发送的内容无关。它只处理如何状态传输。 JSON(您似乎正在使用的协议)将指示需要发送的内容,据我所知,它并没有规定这一点。
  2. 在 JSON 有效负载中包含类型信息实际上取决于您正在使用的库。如果它使您更容易使用 JSON 来包含类型,那么我会说将其放入。如果不是,请将其保留。
  1. REST has nothing to do with the content sent back and forth. It only deals with how the state is transferred. JSON (which is the protocol you seem to be using) would be the one that indicated what would need to be sent, and as far as I know, it doesn't dictate that.
  2. Including the type info in the JSON payload really depends on the libraries you are using. If it makes it easier for you to use JSON to include the types, then I would say put it in. If not, leave it out.
九厘米的零° 2025-01-02 01:59:11

当你有一个模型可以扩展另一个模型时,它真的很有用。指示具体使用哪个模型消除混乱

it's really useful when you have a model that extends another. indicating which model specifically to use eliminate the confusion

夜司空 2025-01-02 01:59:10

每个 REST 对象使用不同的端点 (url)。因此该 url 包含“哪个型号”信息。

每个模型都是变量和(固定)类型的固定集合。

因此通常不需要通过线路发送动态类型信息。

已添加回复 @ali 的评论 -

正确。但您现在问一个不同/更精确的问题:“如何处理 Backbone 模型的初始负载而不引起许多 http 请求?”我不确定这个问题的最佳答案。一种方法是告诉主干下载多个模型集合。

这会将调用次数减少到每个模型一次,而不是每个模型实例一次。

第二种方法是通过非 REST 调用/响应从服务器下载当前数据树。这是个好主意。浏览器客户端可以接收响应,然后将其逐个模型馈送到主干网。请务必向用户提供一些有关正在发生的情况的反馈。

回复:嵌套模型。这是一个SO q

A different endpoint (url) is used for each REST object. So the url includes the "which model" information.

And each model is a fixed collection of variables and (fixed) types.

So there is usually no need to send dynamic type information over the wire.

Added Re the comment from @ali--

Correct. But you're now asking a different/more precise question: "How do I handle the initial load of Backbone models without causing many http requests?" I'm not sure of the best answer to this question. One way would be to tell backbone to download multiple collections of models.

That would reduce the number of calls to one per model vs one per model instance.

A second way would be a non-REST call/response to download the current tree of data from the server. This is a fine idea. The browser-client can receive the response and then feed it model by model into backbone. Be sure to give the user some feedback about what's going on.

Re: nested models. Here's a SO q on it.

甜警司 2025-01-02 01:59:10

考虑到,正如其他答案中已经说过的那样,在 REST 中,每个资源都有自己的端点,因此您尝试做的事情(即将所有模型隐藏在单个端点后面)并不完全符合 REST 规范,恕我直言。本身没什么大不了的。

嵌套集合可能就是这里的答案。

“包装器”集合在初始化时从单个端点获取所有模型,并将它们推送到各自的集合。当然,您必须在 json 中发送类型信息。

从那时起,每个“内部”集合都会对其自己的事件做出反应,并处理自己的端点。

只要您意识到这一点,我不认为这种优化有什么大问题。

Consider that, as already said in other answers, in REST each resource has its own endpoint, and thus what you are trying to do (ie. hide all your models behind a single endpoint) is not fully REST-compliant, IMHO. Not a big deal per se.

Nested collections could be the answer here.

The "wrapper" collection fetches all the models from a single endpoint at init time, and pushes them to the respective collections. Of course you must send the type info in the json.

From that point on, each "inner" collection reacts to its own events, and deals with its own endpoint.

I don't see huge problems with such an optimization, as long as you are aware of it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文