Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring MVC: how to get case-insensitive ordering from Pageable

I am trying to support case-insensitive ordering in my Spring MVC app when users click on the column headings on my web page. When the page is rendered a Thymeleaf extension creates an anchor and the href is the current URL with some parameters supported by Pageable: i.e. page, size and sort.

The sort=propertyName,ASC format works fine, but I can't find out how to say that the sort should be case-insensitive from the URL. I can do it in code easily enough but the standard Pageable support doesn't seem to support it.

After some debugging it appears that the standard framework org.springframework.data.web.SortHandlerMethodArgumentResolver just doesn't have any support for org.springframework.data.domain.Sort.Order.ignoreCase.

I'm somewhat bemused about this, and am wondering if there's a good reason why?

I can look into creating my own SortHandlerMethodArgumentResolver class, and make it parse ASCI|DESCI (to mean case-insensitive), and ASCS|DESCS (to mean case-sensitive) and produce the appropriate Sort object, but this strikes me as quite a bit of work and a serious "code smell".

I can't be the first person to stumble across this. Does anyone have any advice?

like image 635
Marcus Avatar asked May 05 '15 17:05

Marcus


1 Answers

I think the only option is to implement your custom SortHandlerMethodArgumentResolver. The documentation has brief guideline for this http://docs.spring.io/spring-data/data-commons/docs/1.6.1.RELEASE/reference/html/repositories.html

To customize this behavior extend either SpringDataWebConfiguration or the HATEOAS-enabled equivalent and override the pageableResolver() or sortResolver() methods and import your customized configuration file instead of using the @Enable-annotation.

For the format I would make it a comma-separated string of 3 elements: field name, direction, ignoreCase flag. Something like this:

sort=name,ASC,ignore

The last element is optional so it's possible to have:

sort=name,ASC

which would mean that ignoreCase is false.

Also it should be possible to specify only field name like:

sort=name

which would mean the default direction of ASC and ignoreCase is false.

The only issue is if you want to pass ignoreCase flag you must pass the direction which should not be a big problem I think.

Hope this helps!

Btw here is a JIRA item for this improvement https://jira.spring.io/browse/DATACMNS-658 (Extend SortHandlerMethodArgument resolver to be able to detect the request for ignore-case)

like image 127
medvedev1088 Avatar answered Sep 29 '22 22:09

medvedev1088