Skip to content
Advertisement

How to set IgnoreCase on sort parameters from an http request in Spring Boot 1.4

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

Example: http://www.myserver.com/devices/

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

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

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.

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;
        }
    }
}

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

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.

UPDATE:

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.

Advertisement

Answer

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.

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.

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.

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

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement