Spring Data Rest - 按多个属性排序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33018127/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 00:49:00  来源:igfitidea点击:

Spring Data Rest - Sort by multiple properties

springspring-dataspring-data-jpaspring-data-rest

提问by rakpan

I have an entity as below

我有一个实体如下

Class Person{
String id;
String name;
String numberOfHands;
}

With Spring Data Rest (Gosling Release Train), I'm able to specify

使用 Spring Data Rest(Gosling Release Train),我可以指定

localhost/Person?sort=name,asc

for sorting name name ascending. Now, in a case where I need to sort by numberOfHands descending and name ascending. I'm able to specify

用于排序名称名称升序。现在,在我需要按 numberOfHands 降序和名称升序排序的情况下。我可以指定

localhost/Person?sort=numberOfHands,name,asc

But, I'm not able to specify

但是,我无法指定

localhost/Person?sort=numberOfHands,desc,name,asc

Is there a way to specify multiple sort order?

有没有办法指定多个排序顺序?

Thanks!

谢谢!

回答by M. Deinum

Solution (tl;dr)

解决方案(tl;博士)

When wanting to sort on multiple fields you simply put the sortparameter multiple times in the URI. For example your/uri?sort=name,asc&sort=numberOfHands,desc. Spring Data is then capable of constructing a Pageableobject with multiple sorts.

当想要对多个字段进行排序时,您只需将sort参数多次放入 URI 中即可。例如your/uri?sort=name,asc&sort=numberOfHands,desc。然后,Spring Data 能够构造Pageable具有多种类型的对象。

Explanation

解释

There is not really a defined standard on how to submit multiple values for a parameter in a URI. See Correct way to pass multiple values for same parameter name in GET request.

关于如何为 URI 中的参数提交多个值并没有真正定义的标准。请参阅在 GET 请求中为相同参数名称传递多个值的正确方法

However there is some information in the Java Servlet Specwhich hints on how Java servlet containers parse request parameters.

然而,Java Servlet Spec 中有一些信息暗示了 Java servlet 容器如何解析请求参数。

The getParameterValuesmethod returns an array of Stringobjects containing all the parameter values associated with a parameter name. ... - Java Servlet Spec, section 3.1

getParameterValues方法返回一个String对象数组,其中包含与参数名称关联的所有参数值。... - Java Servlet 规范,第 3.1 节

The sample further in that section states (although it mixes request and body data)

该部分中的示例进一步说明(尽管它混合了请求和正文数据)

For example, if a request is made with a query string of a=helloand a post body of a=goodbye&a=world, the resulting parameter set would be ordered a=hello, goodbye, world.

例如,如果使用查询字符串a=hello和帖子正文发出请求a=goodbye&a=world,则生成的参数集将被排序a=hello, goodbye, world

This sample shows that when a parameter (ain the example) is presented multiple times the results will be aggregated into a String[].

此示例显示,当一个参数(a在示例中)被多次呈现时,结果将被聚合到一个String[].

回答by Pim Hazebroek

Here is how to construct the multi Sort object manually/programatically.

以下是如何手动/以编程方式构造多 Sort 对象。

Sort sort = Sort.by(
    Sort.Order.asc("name"),
    Sort.Order.desc("numberOfHands"));
return personRepository.findAll(sort);

Note: This solution does not directly solve the original question asked, but may help visitors that landed on this question while searching for a solution how to sort on multiple properties from a backend perspective / in a somewhat "hardcoded" way. (this solution does not require/take any URI parameters)

注意:此解决方案不会直接解决最初提出的问题,但可以帮助在搜索解决方案时遇到此问题的访问者如何从后端角度/以某种“硬编码”方式对多个属性进行排序。(此解决方案不需要/采用任何 URI 参数)