Spring Data REST的QueryDSL集成可以用于执行更复杂的查询吗? [英] Can Spring Data REST's QueryDSL integration be used to perform more complex queries?

查看:242
本文介绍了Spring Data REST的QueryDSL集成可以用于执行更复杂的查询吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在构建REST API,希望客户端可以轻松地根据特定实体的大多数属性进行过滤.结合使用 QueryDSL Oliver Gierke的示例)通过允许客户端通过组合引用属性的查询参数(例如/users?firstName=Dennis&lastName=Laumen)进行过滤,使我轻松达到所需的90%.

I'm currently building a REST API in which I want clients to easily filter on most properties of a specific entity. Using QueryDSL in combination with Spring Data REST (an example by Oliver Gierke) allows me to easily get to 90% of what I want by allowing clients to filter by combining query parameters which refer to properties (e.g. /users?firstName=Dennis&lastName=Laumen).

我甚至可以通过实现QuerydslBinderCustomizer接口(例如,区分大小写或部分字符串匹配)来自定义查询参数和实体属性之间的映射.这一切都很好,但是我也希望客户端能够使用范围过滤某些类型.例如,关于诸如出生日期之类的属性,我想做以下事情,/users?dateOfBirthFrom=1981-1-1&dateOfBirthTo=1981-12-31.基于数字的属性/users?idFrom=100&idTo=200也是如此.我觉得使用QuerydslBinderCustomizer接口应该可以实现,但是这两个库之间的集成并未得到广泛的记录.

I can even customize the mapping between the query parameters and an entity's properties by implementing the QuerydslBinderCustomizer interface (e.g. for case insensitive searches or partial string matches). This is all great, however I also want the clients to be able to filter some types using ranges. For example with regards to a property like date of birth I'd like to do something like the following, /users?dateOfBirthFrom=1981-1-1&dateOfBirthTo=1981-12-31. The same goes for number based properties, /users?idFrom=100&idTo=200. I have the feeling this should be possible using the QuerydslBinderCustomizer interface but the integration between these two libraries isn't documented very extensively.

总结,是否可以使用Spring Data REST和QueryDSL?如果是这样,怎么办?

Concluding, is this possible using Spring Data REST and QueryDSL? If so, how?

推荐答案

我认为您应该可以使用以下自定义功能来使它起作用:

I think you should be able to get this to work using the following customization:

bindings.bind(user.dateOfBirth).all((path, value) -> {

  Iterator<? extends LocalDate> it = value.iterator();
  return path.between(it.next(), it.next());
});

这里的关键是使用?dateOfBirth=…&dateOfBirth=(两次使用属性)和….all(…)绑定,这将使您可以访问提供的所有值.

The key here is to use ?dateOfBirth=…&dateOfBirth= (use the property twice) and the ….all(…) binding which will give you access to all values provided.

确保将@DateTimeFormat注释添加到UserdateOfBirth属性中,以便Spring能够将传入的Strings正确转换为LocalDate实例.

Make sure you add the @DateTimeFormat annotation to the dateOfBirth-property of User so that Spring is able to convert the incoming Strings into LocalDate instances correctly.

lambda当前得到一个Collection<? extends T>,这使各个元素的纠缠变得更加复杂,但我认为我们可以在将来的版本中对此进行更改以更确切地公开List.

The lambda currently gets a Collection<? extends T> which makes untangling the individual elements a bit more pain that it needs to be, but I think we can change this in a future release to rather expose a List.

这篇关于Spring Data REST的QueryDSL集成可以用于执行更复杂的查询吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆