为什么没有使用 Spring Data JPA 设置版本属性?
想知道 Spring Data REST 中的 @Version
注释是如何用于 ETags 的,但由于某种原因我没有看到 ETags 填充
Wanted to know how is @Version
annotation in Spring Data REST put to use for ETags, I do not see the ETags populated for some reason
@Entity
@EntityListeners(AuditingEntityListener.class)
public class Venue implements Serializable {
private static final long serialVersionUID = -5516160437873476233L;
private Long id;
...
// other properties
private Long version;
private Date lastModifiedDate;
// getters & setters
@JsonIgnore
@LastModifiedDate
public Date getLastModifiedDate() {
return lastModifiedDate;
}
@Version
@Column
public Long getVersion() {
return version;
}
按照文档,这应该会给我一个 Etag 值吗?如库中的片段所示
Going by the docs this should give me an Etag Value? as seen in the snippet from the library
protected HttpHeaders prepareHeaders(PersistentEntity<?, ?> entity, Object value) {
// Add ETag
HttpHeaders headers = ETag.from(entity, value).addTo(new HttpHeaders());
// Add Last-Modified
AuditableBeanWrapper wrapper = getAuditableBeanWrapper(value);
但是,给定实体 &以下配置,我仍然得到一个空版本.我的应用程序有以下
however, given the entity & following configuration, I still get a null for Version. My Application has the following
@SpringBootApplication
@EnableEntityLinks
@EnableJpaAuditing
public class GabbarSinghApplication
而Rest Repository如下
And the Rest Repository is as follows
@RepositoryRestResource(collectionResourceRel = "venue", path = "venues")
public interface VenueRepository extends JpaRepository<Venue, Long> {
虽然我还没有使用标题等测试这些方法,但在 http://localhost:8080/workshops
上的简单 POST 请求会给出 500
因为从版本属性的值获取 ETag 标头值时出现空指针异常.
While I haven't got to test these methods yet with the headers etc, a simple POST request on http://localhost:8080/workshops
gives a 500
because of null pointer exception at getting ETag header value from value of version property.
更新
实体已移至 @javax.persistence.Version,但我仍然没有在响应标头中获得 ETag 标头.
Moved to @javax.persistence.Version for the entities, I still do not get an ETag header in the response headers.
这是一个失败的单元测试
Here's a failing unit test
@Before
public void setUp() throws Exception {
XStream xstream = new XStream();
ObjectInputStream in = xstream.createObjectInputStream(venuesXml.getInputStream());
leela = (Venue) in.readObject();
paul = (Venue) in.readObject();
taj = (Venue) in.readObject();
LOGGER.debug("Initialised Venues from xml file {}", venuesXml.getFilename());
}
@Test
public void testEtagHeaderIsAutoGeneratedOnResourceCreation() {
final HttpEntity<Venue> httpEntity = new HttpEntity<Venue>(taj, headers);
ResponseEntity<ResourceSupport> response = restTemplate.exchange(BASE_LOCATION
+ VENUES_ENDPOINT, HttpMethod.POST, httpEntity,
new ParameterizedTypeReference<ResourceSupport>() {
});
assertTrue("Response should contain ETag header", null != response.getHeaders().getETag());
此断言失败.
推荐答案
我遇到了同样的问题,经过几个小时后,我意识到了以下导致我启蒙的事情 =P
I faced the same problem and after hours and hours I realized the following things that led me to the enlightenment =P
- Spring Data Rest 仅在 2.3.0 版本之后为乐观并发控制提供 ETag 支持.请参阅大约一年前发布的此错误.以前版本的 Spring Data Rest 不会填充 ETag 标头.
- 对于 Spring Boot 应用程序(我们的案例),您需要使用 Spring Boot 1.3.0.BUILD-SNAPSHOT 或更高版本才能设置依赖于 Spring Data Rest 的 spring-boot-starter-data-rest2.4.1(高于 2.3.1,这正是我们所需要的:P).
- Spring Data Rest provides ETag support for optimistic concurrency control ONLY after version 2.3.0. See this bug published about a year ago. Previous versions of Spring Data Rest will NOT populate the ETag header.
- For Spring Boot applications (our case), you need to use Spring Boot 1.3.0.BUILD-SNAPSHOT or higher in order to be able to setup a spring-boot-starter-data-rest that depends on Spring Data Rest 2.4.1 (higher than 2.3.1 which is exactly what we need :P).
在我的情况下,我使用的是 Spring Boot 1.2.7,每当我安装 spring-boot-starter-data-rest 依赖项时,我最终都会得到不支持 ETag 的 Spring Data Rest 2.2.0.在我的项目中升级 Spring Boot 并重新安装依赖项后,我的 REST API 开始检索 ETag 标头 =D
In my case I was using Spring Boot 1.2.7 and whenever I installed the spring-boot-starter-data-rest dependency I ended up getting the Spring Data Rest 2.2.0 which don't has the ETag support. After upgrading Spring Boot in my project and reinstalled the dependencies my REST API started to retrieve the ETag header =D
相关文章