Spring Data Rest - 按多个属性排序 [英] Spring Data Rest - Sort by multiple properties

查看:25
本文介绍了Spring Data Rest - 按多个属性排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个实体如下

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

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

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

localhost/Person?sort=name,asc

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

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

localhost/Person?sort=numberOfHands,name,asc

但是,我无法指定

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

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

Is there a way to specify multiple sort order?

谢谢!

推荐答案

Solution (tl;dr)

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

Solution (tl;dr)

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

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

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.

然而,Java Servlet Spec 中有一些信息提示关于 Java servlet 容器如何解析请求参数.

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

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

The getParameterValues method returns an array of String objects containing all the parameter values associated with a parameter name. ... - Java Servlet Spec, section 3.1

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

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

例如,如果使用查询字符串 a=hello 和帖子正文 a=goodbye&a=world 发出请求,则结果参数集将被订购a=hello, goodbye, world.

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

此示例显示,当一个参数(示例中的a)出现多次时,结果将聚合为一个String[].

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

这篇关于Spring Data Rest - 按多个属性排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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