Spring Data REST 如何将文档的版本和 ID 发送到 REST API 客户端



我们在 spring 启动应用程序中使用 mongodb 和 spring data rest。我们通过 spring data rest 默认的 REST API 公开文档。对于乐观锁定,每个文档都带有 @org.springframework.data.annotation.Version@org.springframework.data.annotation.Id 注释。

我想知道默认情况下如何通过 REST API 公开这些属性,以便客户端可以更新文档。

关于 id:默认情况下,spring-data-rest 隐藏 id,并尝试仅与资源链接进行通信。它试图应用HATEAOS的原则 - http://docs.spring.io/spring-data/rest/docs/current/reference/html/#conditional.etag

关于版本:如果你在你的实体中指定了一个版本(我只有spring-data-jdbc背景),spring-data-rest将在ETag标头的响应中报告该版本。

例如,如果您发布补丁并希望确保您更新的版本仍然是最新的,然后才能使用您在If-Match标头中收到的ETag。如果版本不再是最新的,您将收到一条412 Precondition Failed

所以这是我的请求流程:

//get a product
http :8080/products/2 -v
Response:
HTTP/1.1 200 OK
ETag: "2"

补丁请求如下所示

http PATCH :8080/products/2 name=some3 If-Match:2 -v
Request:
  PATCH /products/2 HTTP/1.1
  Accept: application/json
  Content-Type: application/json
  If-Match: 1
Response:
  HTTP/1.1 412 Precondition Failed
  ETag: "3"

您可以在 spring-data-rest 文档中找到详细信息:http://docs.spring.io/spring-data/rest/docs/current/reference/html/#conditional.etag

最新更新