Spring 数据休息和计数查询
Spring Data Rest and count queries
我正在使用 Spring Data Rest 试验几行代码。我找不到任何东西可以通过 REST 发送一个简单的请求来计算特定实体的所有记录。
假设我有以下实体:
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class Person {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
private String firstName;
private String lastName;
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
}
应使用以下存储库访问:
import java.util.List;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.data.repository.query.Param;
import org.springframework.data.rest.core.annotation.RepositoryRestResource;
@RepositoryRestResource(collectionResourceRel = "people", path = "people")
public interface PersonRepository extends PagingAndSortingRepository<Person, Long> {
}
有没有办法通过 REST 了解我的数据库中保存了多少 Person
实体,没有指定条件?
看起来 CrudRepository 的 count() 方法是您的候选。根据 javadoc:
/**
* Returns the number of entities available.
*
* @return the number of entities
*/
long count();
Spring 数据 REST serves 仅标准 HTTP 方法,例如 GET、POST、PUT、PATCH、DELETE 等
但是如果你需要获取资源总数,你可以,例如,发出 GET 请求并检查它的页面块:
"page" : {
"size" : 5,
<b>"totalElements" : 50,</b>
"totalPages" : 10,
"number" : 1
}
或创建 custom controller.
已更新
对于 GET method on collection resources SDR returns 具有资源元素总数的可分页结果。 SDR 触发两个 select 查询:一个针对页面上的元素,第二个 - count 查询所有元素。
另一种选择是引入自定义查询方法。这些方法通过 REST 数据存储库公开。
例如,使用 JPA:
@RepositoryRestResource(collectionResourceRel = "people", path = "people")
public interface PersonRepository extends PagingAndSortingRepository<Person, Long> {
@Query("select count(p) from Person p")
Long findCount();
}
使用MongoDB:
@RepositoryRestResource(collectionResourceRel = "people", path = "people")
public interface PersonRepository extends MongoRepository<Person, Long> {
@Query(value = "{}", count = true)
Long findCount();
}
然后从 rest 调用它,它只是 /people/search/findCount
,returns 只是正文中的数字,没有额外的属性,例如:
200 success
date: Wed, 09 May 2018 17:59:13 GMT
transfer-encoding: chunked
content-type: application/hal+json;charset=UTF-8
154
无需解析 GET 响应和不必要的获取记录。
我正在使用 Spring Data Rest 试验几行代码。我找不到任何东西可以通过 REST 发送一个简单的请求来计算特定实体的所有记录。
假设我有以下实体:
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class Person {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
private String firstName;
private String lastName;
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
}
应使用以下存储库访问:
import java.util.List;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.data.repository.query.Param;
import org.springframework.data.rest.core.annotation.RepositoryRestResource;
@RepositoryRestResource(collectionResourceRel = "people", path = "people")
public interface PersonRepository extends PagingAndSortingRepository<Person, Long> {
}
有没有办法通过 REST 了解我的数据库中保存了多少 Person
实体,没有指定条件?
看起来 CrudRepository 的 count() 方法是您的候选。根据 javadoc:
/**
* Returns the number of entities available.
*
* @return the number of entities
*/
long count();
Spring 数据 REST serves 仅标准 HTTP 方法,例如 GET、POST、PUT、PATCH、DELETE 等
但是如果你需要获取资源总数,你可以,例如,发出 GET 请求并检查它的页面块:
"page" : {
"size" : 5,
<b>"totalElements" : 50,</b>
"totalPages" : 10,
"number" : 1
}
或创建 custom controller.
已更新
对于 GET method on collection resources SDR returns 具有资源元素总数的可分页结果。 SDR 触发两个 select 查询:一个针对页面上的元素,第二个 - count 查询所有元素。
另一种选择是引入自定义查询方法。这些方法通过 REST 数据存储库公开。
例如,使用 JPA:
@RepositoryRestResource(collectionResourceRel = "people", path = "people")
public interface PersonRepository extends PagingAndSortingRepository<Person, Long> {
@Query("select count(p) from Person p")
Long findCount();
}
使用MongoDB:
@RepositoryRestResource(collectionResourceRel = "people", path = "people")
public interface PersonRepository extends MongoRepository<Person, Long> {
@Query(value = "{}", count = true)
Long findCount();
}
然后从 rest 调用它,它只是 /people/search/findCount
,returns 只是正文中的数字,没有额外的属性,例如:
200 success
date: Wed, 09 May 2018 17:59:13 GMT
transfer-encoding: chunked
content-type: application/hal+json;charset=UTF-8
154
无需解析 GET 响应和不必要的获取记录。