我们在一个spring引导应用程序中使用mongodb和spring数据rest。我们通过spring数据rest默认REST公开文档。对于乐观锁定,每个文档都使用@org.springframework.data.annotation.Version和@org.springframework.data.annotation.Id进行注释。
我想知道默认情况下如何通过REST公开这些属性,以便客户端可以更新文档。
发布于 2016-01-15 11:08:23
有关id: spring-data-rest默认情况下隐藏id,并尝试仅与资源链接进行通信。它试图应用HATEAOS - http://docs.spring.io/spring-data/rest/docs/current/reference/html/#conditional.etag的原理。
有关version:如果您的实体中指定了一个版本(我只有spring jdbc背景),spring rest将在ETag头部的响应中报告该版本。
例如,如果您发布修补程序,并希望确保您更新的版本仍然是最新的,那么在您可以使用在ETag头中收到的If-Match之前,您所读的内容仍然是最新的。如果该版本不再是最新版本,您将收到一个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。
https://stackoverflow.com/questions/34787568
复制相似问题