具有以下存储库:
@RepositoryRestResource(collectionResourceRel = "people", path = "people")
public interface PeopleRepository extends PagingAndSortingRepository<People, String> {
@RestResource
List<People> findByName(@Param("name") String name);
}
findByName
的URL自动设置为/people/search/findByName
。但是似乎很冗长,可以将URL配置为/people
并查询像/people?name=john
吗?
答案 0 :(得分:2)
如果您使用QueryDSL扩展名,则可以使用该格式的查询,并具有可以通过属性的任意组合进行过滤而无需编写任何查询方法的额外好处:
配置(假设为Maven)
<dependency>
<groupId>com.querydsl</groupId>
<artifactId>querydsl-apt</artifactId>
<version>4.1.4</version>
</dependency>
<dependency>
<groupId>com.querydsl</groupId>
<artifactId>querydsl-jpa</artifactId>
<version>4.1.4</version>
</dependency>
然后只需更新您的存储库定义:
@RepositoryRestResource(collectionResourceRel = "people", path = "people")
public interface PeopleRepository extends PagingAndSortingRepository<People, String>,
QuerydslPredicateExecutor<People> {
}
您现在应该可以进行以下过滤:
/people?name=john
/people?name=John&address.town=London
等
答案 1 :(得分:0)