【发布时间】:2016-08-19 14:20:57
【问题描述】:
在 Spring Data REST(通过 Spring Boot 1.3.3)中,当我 GET 一个资源集合(例如 people)时,@Version 属性不包含在资源中:
$curl -v http://localhost:8080/api/people/1
* Trying ::1...
* Connected to localhost (::1) port 8080 (#0)
> GET /api/people/1 HTTP/1.1
> Host: localhost:8080
> User-Agent: curl/7.42.1
> Accept: */*
>
< HTTP/1.1 200 OK
< Server: Apache-Coyote/1.1
< ETag: "0"
< Last-Modified: Tue, 26 Apr 2016 00:08:12 GMT
< Content-Type: application/hal+json;charset=UTF-8
< Transfer-Encoding: chunked
< Date: Tue, 26 Apr 2016 00:12:56 GMT
<
{
"id" : 1,
"createdDate" : {
"nano" : 351000000,
"epochSecond" : 1461629292
},
"lastModifiedDate" : {
"nano" : 351000000,
"epochSecond" : 1461629292
},
"firstName" : "Bilbo",
"lastName" : "Baggins",
"_links" : {
"self" : {
"href" : "http://localhost:8080/api/people/1"
},
"person" : {
"href" : "http://localhost:8080/api/people/1"
}
}
* Connection #0 to host localhost left intact
默认情况下,或者当我配置我的 Spring Data 存储库时:
@Configuration
public class ApplicationRepositoryConfiguration
extends RepositoryRestMvcConfiguration
{
@Override
protected void configureRepositoryRestConfiguration(
RepositoryRestConfiguration config
)
{
config.exposeIdsFor(Person.class);
config.setBasePath("/api/");
}
}
@Version 是更新时递增的数据行版本,并在我查询特定资源时包含在ETag HTTP 标头 数据中。而不是必须在集合中的每个资源上调用GET,我宁愿在集合GET 中获取@Version,这样我就可以编写我的应用程序来检查每个资源更新时的@Version 值没有执行n添加GET往返。
有没有办法将@Version 字段包含在集合GET 的每个资源中?
实体定义如下所示:
@Data @Entity @EntityListeners(AuditingEntityListener.class)
public class Person {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
@CreatedDate
@Column(nullable=false)
private Instant createdDate;
@LastModifiedDate
@Column(nullable=false)
private Instant lastModifiedDate;
@Version
@JsonProperty
private Long version;
…
}
【问题讨论】:
标签: spring rest spring-boot spring-data spring-data-rest