[英]Advanced search in a Rest Api using spring / querydsl
I'm using Spring Data / QueryDSL with web support, using the annotation @EnableSpringDataWebSupport
.我正在使用带有 web 支持的 Spring 数据/QueryDSL,使用注释
@EnableSpringDataWebSupport
。 This works well, and automatically maps a GET query to a Predicate
.这很好用,并自动将 GET 查询映射到
Predicate
。 I can search my DTP objects using a query such as:我可以使用以下查询搜索我的 DTP 对象:
http://localhost/search/dtp?name=foo
I now need to add more complex queries, such as AND or OR clauses.我现在需要添加更复杂的查询,例如 AND 或 OR 子句。
I found this library which seems to achieve what I want: spring-data-querydsl-value-operators我发现这个库似乎实现了我想要的: spring-data-querydsl-value-operators
My understanding is that I need to add the following code to my Repository interface to leverage this library:我的理解是,我需要将以下代码添加到我的 Repository 接口以利用该库:
@Override
default void customize(QuerydslBindings bindings, QDtp root) {
bindings.bind(root.name).all(ExpressionProviderFactory::getPredicate);
bindings.bind(root.description).all(ExpressionProviderFactory::getPredicate);
...
}
However I didn't need to have a customize()
method before, and now it seems I need to have these new bindings for all the fields and sub-fields of my object.但是我以前不需要
customize()
方法,现在看来我需要为我的 object 的所有字段和子字段设置这些新绑定。 This could lead to maintaining issues: if a new field is added but the developer forgets to add this binding, then the search on that field won't work as with the other fields.这可能会导致维护问题:如果添加了新字段但开发人员忘记添加此绑定,则对该字段的搜索将无法与其他字段一样工作。
This wasn't the case previously.以前不是这种情况。
How could I make it so that these bindings are generic and applied to all fields and subfields of my object?我怎样才能使这些绑定通用并应用于我的 object 的所有字段和子字段?
I think that you may just use this library: https://github.com/turkraft/spring-filter我认为您可能只使用这个库: https://github.com/turkraft/spring-filter
It will let you run search queries such as:它可以让您运行搜索查询,例如:
/search?filter= average (ratings) > 4.5 and brand.name in ('audi', 'land rover') and (year > 2018 or km < 50000) and color : 'white' and accidents is empty
/search?filter= average (ratings) > 4.5 and brand.name in ('audi', 'land Rover') and (year > 2018 or km < 50000) and color : 'white' and associates are empty
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.