【问题标题】:How does spring data rest send the version and the id of a document to the REST API client?spring data rest如何将文档的版本和id发送到REST API客户端?
【发布时间】:2016-01-14 10:45:29
【问题描述】:

我们在 Spring Boot 应用程序中使用 mongodb 和 Spring Data Rest。我们通过 spring data rest 默认 REST API 公开文档。对于乐观锁定,每个文档都标有@org.springframework.data.annotation.Version@org.springframework.data.annotation.Id

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

【问题讨论】:

标签: java spring-data-rest


【解决方案1】:

关于 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 的响应中报告版本。

如果你例如发布补丁并希望确保您更新的版本仍然是您阅读的最新版本,然后才能使用您在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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-03-15
    • 2019-12-27
    • 2019-06-24
    • 2015-03-13
    • 2017-07-10
    • 1970-01-01
    • 2013-10-30
    • 1970-01-01
    相关资源
    最近更新 更多