如何在Spring Boot 1.4中的http请求中为排序参数设置IgnoreCase [英] How to set IgnoreCase on sort parameters from an http request in Spring Boot 1.4

查看:235
本文介绍了如何在Spring Boot 1.4中的http请求中为排序参数设置IgnoreCase的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Spring Boot后端正在运行并接受HTTP请求。

I have a Spring Boot backend running and accepting HTTP requests.

示例: http://www.myserver.com/devices/

我正在使用Pageable来处理请求和自动分页和排序,所以我可以使用如下请求: http://www.myserver .com / devices?sort = name,desc

I'm using Pageable to handle the requests and automatic Pagination and Sorting, so I'm able to use requests like: http://www.myserver.com/devices?sort=name,desc

然而,有一个问题,即如果区分大小写的排序,那么所需的排序结果(case未获得不敏感的排序。

There is an issue however, that the sort if case sensitive, and thus the desired sorting result (case insensitive sorting) is not obtained.

我知道Pageable Sort接受sort属性的两个参数,即sort = name,desc但是在检查调试器时我可以看到可分页的排序顺序对象包含ignoreCase属性。

I know Pageable Sort accepts two arguments for the sort attribute i.e. sort=name,desc but when inspecting the debugger I can see that the pageable's sort orders objects contain an ignoreCase attribute.

以下是通过REST调用方法的代码片段:

Here is a snippet of code for the method that's being called via REST:

public class DeviceController {

    @Autowired
    DeviceViewRepository deviceViewRepository;

    @RequestMapping(method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    public Page<DeviceView> getAllDevices(@PageableDefault(page = 0, size = 10, sort= "id") Pageable pageable) {
        try {
            return deviceViewRepository.findAll(pageable);
        } catch (Exception e){
            throw e;
        }
    }
}

有没有办法设定通过请求网址,PageableDefault或任何其他方法自动/手动?

Is there any way to set that automatically/manually via the request url, PageableDefault, or any other method?

请记住,排序参数是动态的,有问题的方法是使用findAll Spring数据库的()方法。

Please keep in mind that the sort parameters are dynamic and the method in question is using the findAll() method of the Spring Data Repository.

更新:

我'在这里发现了类似的问题: Spring MVC:如何从Pageable获取不区分大小写的排序

I've found a similar issue here: Spring MVC: how to get case-insensitive ordering from Pageable

这基本上就是我的目标。我不能在那里评论它是如何完成的或提示,所以如果有人知道如何实现该解决方案,我真的很感激在正确的方向上推动。

This is essentially what I'm aiming for. I cannot comment there to ask how it was done or for tips, so if anyone knows how to implement that solution, I'd really appreciate a nudge in the right direction.

推荐答案

答案是(遗憾的是)你必须自己用一个解析器处理它 - 至少在 https://jira.spring.io/browse/DATACMNS-658 已根据帖子 Spring MVC:如何从Pageable获得不区分大小写的排序

The answer is (sadly) that you have to handle it yourself with a resolver - at least until https://jira.spring.io/browse/DATACMNS-658 has been resolved, as per the answer to the post Spring MVC: how to get case-insensitive ordering from Pageable.

获得自己的<$ c $相对简单c>要调用的ArgumentResolver ,解析URL并更新Sort对象。我面临的挑战是确保我的分页控件总是使用正确的URL,我不得不做很多我认为不应该进行的URL操作 - 虽然我是Spring的新手,所以我可能并没有做得太好。

It's relatively straightforward to get your own ArgumentResolver to be called, parse the URL and update the Sort object. The challenge for me was to make sure that my paging control always used the correct URL, and I had to do quite a lot of URL manipulation that I didn't think I should have to - though I am a novice at Spring so possibly I didn't do it too well.

我很乐意提供我的实现给你一个headstart,但没有任何地方我可以上传文件留下来在那里任何时间。我不想在这里放一个链接(例如到一个dropbox),当有人再次阅读这篇文章时,这个链接不会在12个月内出现。

I am happy to provide my implementation to give you a headstart, but don't have anywhere I can upload the files that would stay there for any length of time. I'd hate to put a link here (e.g. to a dropbox) that would not be there in 12 months time when someone reads this post again.

很高兴提供者PM和其他人如果有手段就公开发布。

Happy to provide by PM and for someone else to post them publicly if they have the means.

这篇关于如何在Spring Boot 1.4中的http请求中为排序参数设置IgnoreCase的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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