I am using spring boot 2.5.7
Created a webservice using spring boot and using below URL to sort all projects retrieved from the database (sort by projectId attribute, sort direction is descending)
http://localhost:8081/api/projects?sort=projectId,desc
Below is my controller, ProjectPage and PageSort models
@GetMapping
public ResponseEntity<Page<Project>> getAllProjects(
@Valid ProjectPage projectPage,
ProjectSearchCriteria projectSearchCriteria){
return null;
}
public class ProjectPage {
@Valid
private List<PageSort> sort;
}
public class PageSort {
private String sortBy;
private String sortDirection;
}
In order to convert the java.lang.String (projectId,desc passed in the request URL) to PageSort I have used converter SPI documented in https://docs.spring.io/spring-framework/docs/current/reference/html/core.html#core-convert-Converter-API and this works perfectly.
1). I have extended the WebMvcConfigurationSupport
public class WebMvcConfig extends WebMvcConfigurationSupport {
@Override
protected void addFormatters(FormatterRegistry registry) {
registry.addConverter(stringToPageSortConverter());
super.addFormatters(registry);
}
@Bean
public StringToPageSortConverter stringToPageSortConverter(){
return new StringToPageSortConverter();
}
}
2). And implemented the Converter as below
@Component
public class StringToPageSortConverter implements Converter<String, PageSort> {
@Override
public PageSort convert(String source) {
PageSort pageSort;
String[] _sort = source.split(",");
if(_sort.length > 1){
pageSort = PageSort.builder()
.sortBy(_sort[0])
.sortDirection(_sort[1])
.build();
}else{
pageSort = PageSort.builder()
.sortBy(_sort[0])
.sortDirection("asc")
.build();
}
return pageSort;
}
}
When I invoke http://localhost:8081/api/projects?sort=projectId,desc and check the StringToPageSortConverter class I am seeing something as below. (Attaching snippets of the output as it's easier to see the issue graphically)
1). snippet one
2). But when I continue resume from the breakpoint it hits the breakpoint again.
In the final projectPage variable in the controller now have two sort objects which are completely wrong. Should have been a single PageSort(sortBy=projectId, sortDirection=desc)
I am not sure whether I am missing something here.
Comment From: wilkinsona
Spring Boot 2.5.x is no longer supported. Please upgrade to 2.7.x. If the problem still occurs and you would like us to spend some more time investigating, please spend some time providing a complete yet minimal sample that reproduces the problem. You can share it with us by pushing it to a separate repository on GitHub or by zipping it up and attaching it to this issue.
Comment From: ssomlk
I tried this in Spring boot 2.7.9 version and the issue still exists. I will provide a complete minimal sample code at earliest.
Thanks
Comment From: ssomlk
WebMvcConfigurationSupport.zip
Attached the project with minimal code herewith.
Please note that;
1) When the request URL is http://localhost:8081/api/projects?sort=projectId,desc&sort=projectName,desc it is working as expected. (See below)
2). Issue is when the request URL is http://localhost:8081/api/projects?sort=projectId,desc (single sort query parameter)
Comment From: wilkinsona
You are binding the sort parameter to a list. When there's a single sort parameter this is a string to collection conversion and the string is treated as a comma-separated string with multiple values. This results in your converter being called once for projectId and once for desc. When there are multiple sort parameters an array to collection conversion is performed and your converter is called with projectId,desc and projectName,desc. In short, this is Spring MVC's standard behavior and is working as designed.
If possible, I would recommend using a delimiter other than comma in the value of the sort parameters. If you must use , for some reason, you will have to disable the string to collection conversion. This Stack Overflow question may help with that.
If you have any further questions, please follow up on Stack Overflow or Gitter. As mentioned in the guidelines for contributing, we prefer to use GitHub issues only for bugs and enhancements.