我正在使用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
只是为了更改布尔属性,我必须获取整个对象以获取版本。方便吗有没有更好的方法?
答案是肯定的。我相信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"