0

我有一个在 Google App Engine 上运行的 RESTful Web 服务,并使用 JPA 将实体存储在 GAE 数据存储中。

使用 POST 请求创建新实体(因为服务器将生成实体 ID)。

但是,我不确定返回的最佳状态代码,因为 GAE DS 最终是一致的。我考虑了以下几点:

  • 200 OK:RFC 声明响应正文应包含“描述或包含操作结果的实体”。这是可以实现的,因为实体在持久化到 DS 时会使用其生成的 ID 进行更新,因此可以立即序列化并返回更新的实体。但是,随后按 ID 对该实体的 GET 请求可能会失败,因为所有节点可能尚未达到一致性(这已被视为我的客户端应用程序的现实问题)。
  • 201 Created:如上所述,如果尚未达到一致性,则返回新实体的 URI 可能会导致客户端出现问题。
  • 202 Accepted:将消除上述问题,但无法将新实体的 ID 通知客户端。

在这种情况下,什么被认为是最佳实践?

4

2 回答 2

2

A get by key will always be consistent, so a 200 response would be Ok based on your criteria unless there is a problem in google land. Are you certain you observed problems are from gets rather than queries. There is a difference between a query selecting a KEY vs a GET by key.

For a query to be consistent it must be an ancestor query, alternately a GET is consistent, anything else may see inconsistent data as indexes have yet to be updated.

This is all assuming there isn't an actual problem in google land. We have seen problems in the past, where datacenters where late replicating, and eventual consistancy was very late, sometimes even hours.

But you have no way of knowing that, so you either have to assume all is OK, or take an extremely pessimistic approach.

于 2014-04-08T10:37:30.167 回答
0

这取决于您使用的 JSON REST Protocoll。总是返回一个 json 对象不是很 RESTful。

你应该看看其中的一些:

回答你的问题:我更喜欢使用资源本身知道它的 URL 的格式,所以我会使用 201 但也返回整个资源。

最简单的方法是将 jsonapi 与方便的 url 模式一起使用,因此您可以通过 url 找到资源,因为您知道 id。

于 2014-04-08T10:16:53.160 回答