-
Notifications
You must be signed in to change notification settings - Fork 38.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Why escaped comma is the delimiter for @RequestParam List<String>
?
#23820
Comments
@RequestParam List<String>
?
This is due to the |
Let's be honest, it's just broken. The parameter is sent with a percent escaped (And the RFC even explicitly states that commas are allowed as parameter values as long as they are percent escaped.) |
I managed to solve the problem by applying the following:
The trick is done by the following lines of code @InitBinder
public void initBinder(WebDataBinder binder) {
binder.registerCustomEditor(String[].class, new StringArrayPropertyEditor(null));
} My classes were as follows: Clase Search (works as a wrapper)public class Search {
private String[] search = new String[] {};
public Search() {
super();
}
public Search(String[] search) {
super();
this.search = search;
}
public String[] getSearch() {
return search;
}
public void setSearch(String[] search) {
this.search = search;
}
public List<String> toList() {
return Arrays.asList(this.search);
}
@Override
public String toString() {
return "Search [search=" + Arrays.toString(search) + "]";
}
} Endpoint@InitBinder
public void initBinder(WebDataBinder binder) {
binder.registerCustomEditor(String[].class, new StringArrayPropertyEditor(null));
}
@GetMapping("/search")
@ResponseStatus(HttpStatus.OK)
@ApiOperation(value = "Search providers")
//@formatter:off
@ApiImplicitParams({
@ApiImplicitParam(
name = "search",
allowMultiple = true,
dataType = "string",
paramType = "query",
value = "Search by field: description"
)
})
//@formatter:on
public List<String> search(Search search) {
System.out.println(search.toList().size());
search.toList().forEach(System.out::println);
//..... your code
} Results |
Affects: < Spring Boot 2.1.2.RELEASE>
I have a simple rest controller with the method the get requests mapped on:
I try to pass string hello, world! as a single element of List cis parameter:
I have expected the list contains only one string but аs it turned the list contains two elements although the comma has been escaped in source request.
In this way there's not a difference between requests:
and
So, I'm not able to map parameters of HTTP GET requests to Java List if they contain escaped commas.
The text was updated successfully, but these errors were encountered: