Ana*_*sra 11 java spring spring-data spring-data-jpa spring-data-rest
想知道@VersionSpring Data REST中的注释如何用于ETag,我没有看到由于某种原因填充了ETag
@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;
}
Run Code Online (Sandbox Code Playgroud)
通过文档,这应该给我一个Etag价值?如图书馆的片段所示
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);
Run Code Online (Sandbox Code Playgroud)
但是,鉴于实体和以下配置,我仍然得到版本的null.我的申请表有以下内容
@SpringBootApplication
@EnableEntityLinks
@EnableJpaAuditing
public class GabbarSinghApplication
Run Code Online (Sandbox Code Playgroud)
Rest Repository如下
@RepositoryRestResource(collectionResourceRel = "venue", path = "venues")
public interface VenueRepository extends JpaRepository<Venue, Long> {
Run Code Online (Sandbox Code Playgroud)
虽然我还没有用头文件等测试这些方法,但是一个简单的POST请求在从版本属性的值获取ETag头值时因为空指针异常而http://localhost:8080/workshops给出了500.
更新
移动到实体的@ javax.persistence.Version,我仍然没有在响应头中获得ETag标头.
这是一个失败的单元测试
@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());
Run Code Online (Sandbox Code Playgroud)
这个断言失败了.
Oli*_*ohm 15
使用Spring Data JPA,您需要使用@javax.persistence.Version.@org.springframework.data.annotation.Version是用于其他Spring Data模块的注释.
我遇到了同样的问题,经过几个小时后,我意识到以下事情让我得到了启示=P
就我而言,我使用的是 Spring Boot 1.2.7,每当我安装 spring-boot-starter-data-rest 依赖项时,我最终都会得到不支持 ETag 支持的 Spring Data Rest 2.2.0。在我的项目中升级 Spring Boot 并重新安装依赖项后,我的 REST API 开始检索 ETag 标头 =D
| 归档时间: |
|
| 查看次数: |
3876 次 |
| 最近记录: |