spring data rest 如何将文档的版本和id 发送给REST API 客户端?

How does spring data rest send the version and the id of a document to the REST API client?

我们在 spring 启动应用程序中使用 mongodb 和 spring 数据。我们通过 spring data rest default 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 Header 的响应中报告该版本。

如果你例如发布一个补丁,并希望确保您更新的版本仍然 up-to-date 与您阅读的内容一致,然后才能使用您在 If-Match header 中收到的 ETag。如果版本不再是 up-to-date,您将收到 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