Spring Data REST,如何处理清单中的@Version属性

问题描述 投票:3回答:1

我正在使用Spring Boot 1.5.6和Spring Data REST。我知道这个讨论:With Spring Data REST, why is the @Version property becoming an ETag and not included in the representation?

很明显,为什么SDR将@Version属性转换为Etag。...但是这种常见情况呢:我有一个实体列表(我使用GET获取数据),并且想要进行逻辑删除其中之一。因此,我将对该特定实体执行PATCH。为了执行PATCH,我需要设置If-None-Match标头,因此我需要知道版本。

在这种情况下,我用GET来获取列表,但是要使对象的PATCH只是为了更改布尔属性,我必须获取整个对象以获取版本。方便吗有没有更好的方法?

java spring rest spring-mvc spring-data-rest
1个回答
0
投票

答案是肯定的。我相信Spring / REST应该将它作为一级属性返回到实体主体中,但是REST混淆了两个不同的问题:实体版本控制和HTTP消息传递。解决方案是公开一个恰好容纳对象版本的新“ phantom”属性:

@Version
private int version; // the entity version, managed by JPA

public int getVersion() { return this.version; } // Spring suppresses this
public int getVisibleVersion() { return this.version; } // appears in entity JSON as "visibleVersion"
© www.soinside.com 2019 - 2024. All rights reserved.