Spring Data Rest - 具有默认值的参数 [英] Spring Data Rest - Parameters with default values
问题描述
我创建了以下 @RepositoryRestResource
查询,我想在其中为我的 rest api 创建动态查询.所以基本上我想做类似的事情:
I have created the following @RepositoryRestResource
query where I want to create a dynamic query for my rest api. So basically I would want to do something like:
myHost/myApp/data/search/all?name=me&age=20&address=myhome&etc=etc
所以我创建了以下查询:
So I have created the query below:
@Query("Select t from Data t " +
"where " +
"t.name like :name AND " +
"t.age = :age AND " +
"t.address = :address AND " +
"t.etc= :etc"
@RestResource(path = "all", rel = "all")
Page findByAll(@Param("name") String name, @Param("age") String age,
@Param("address") String address, @Param("etc") String etc, Page page);
显然,其中一些可能尚未输入.有没有办法在存储库上定义默认值?例如,我希望 name
的默认值为 %
.
Obviously some of these may not have been entered. Is there some way to define default values on the Repository? So for example I would want name
to have a default value of %
.
我不完全确定这种方法是否适合我想要做的事情,因此欢迎提供任何其他建议.
I'm not entirely sure this approach is the correct one for what I want to do, so any alternative suggestions are welcome.
推荐答案
我知道这是旧的,但我遇到了类似的问题并解决了如下:
I know this is older, but I had a similar issue and solved it as follows:
@Query("Select t from Data t " +
"where " +
"(:name IS NULL or t.name like :name) AND " +
"(:age IS NULL or t.age = :age) AND " +
"(:address IS NULL or t.address = :address) AND " +
"(:etc IS NULL or t.etc= :etc)")
@RestResource(path = "all", rel = "all")
Page findByAll(@Param("name") String name, @Param("age") String age,
@Param("address") String address, @Param("etc") String etc, Page page);
这篇关于Spring Data Rest - 具有默认值的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!