问题描述
我正在使用spring-data-rest,并且会像PUT /{repository}/{id}/{property}
Body:
/12
Accept: application/json
Content-Type: text/uri-list
If-Match: 42 (incorrect value)
我希望收到412错误,但返回204。
调试时,我发现DispatcherServlet使用HandlerExecutionChain with [org.springframework.data.rest.webmvc.RepositoryPropertyReferenceController#createPropertyReference(RootResourceInformation,HttpMethod,CollectionModel,Serializable,String)] and 3 interceptors
,并且该方法不检查if-Match值。
只有诸如PUT,PATCH,DELETE /{repository}/{id}
之类的请求才使用HandlerExecutionChain with [org.springframework.data.rest.webmvc.RepositoryEntityController#patchItemResource(RootResourceInformation,PersistentEntityResource,PersistentEntityResourceAssembler,ETag,String)] and 3 interceptors
来检查if-Match值。
能否使spring-data-rest验证是否为PUT,DELETE和PATCH /{repository}/{id}/{property}
的if-Match值?
解决方法
根据official documentation,Spring Data REST需要一个带有@Version
注释的版本字段才能实现ETag。
如果要对嵌入式属性(字段)进行版本控制,如何对单个嵌入式属性进行单独版本控制?答案是不可能的。
如果要版本化引用的属性(JPA连接),则应向/ {that-property-repository} / {id}而不是/ {repository} / {id}发出PUT / PATCH / DELETE请求/ {属性}。