0

We are using mongodb and spring data rest in a spring boot app. We expose the documents via spring data rest default REST API. For optimistic locking each document is annotated with @org.springframework.data.annotation.Version and @org.springframework.data.annotation.Id.

I wonder how these attributes are exposed by default through the REST API so that the client can update a document.

René Reitmann
  • 364
  • 2
  • 17
  • Maybe this question can help you: http://stackoverflow.com/questions/18780621/does-spring-data-rest-support-jpa-version – Rozart Jan 14 '16 at 11:07

1 Answers1

1

Regarding id: spring-data-rest hides the id by default and tries to communicate only with links to resources. It tries to apply the principles of HATEAOS - http://docs.spring.io/spring-data/rest/docs/current/reference/html/#conditional.etag

Regarding version: If you have a version specified in your entity (I only have spring-data-jdbc background) spring-data-rest will report the version in the response in the ETag Header.

If you e.g. issue a patch and want to make sure the version you update is still up-to-date with what your read before you can use the ETag you received in the If-Match header. If the version is no longer up-to-date you receive a 412 Precondition Failed

So here is my request flow:

//get a product
http :8080/products/2 -v

Response:
HTTP/1.1 200 OK
ETag: "2"

The patch request would look like this

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"

You will find details in the spring-data-rest documentation: http://docs.spring.io/spring-data/rest/docs/current/reference/html/#conditional.etag

Mathias Dpunkt
  • 11,594
  • 4
  • 45
  • 70