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

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

问题描述

我有一个如下实体

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

使用Spring Data Rest(Gosling发布培训),我可以指定

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?

谢谢!

推荐答案

解决方案(tl; dr)

要对多个字段进行排序时,只需将sort参数多次放在URI中.例如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规范中有一些信息提示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天全站免登陆