Having this http://myserver/find-by-phones?phone=123&phone=345
request, is it possible to handle with something like this:
@Controller
public class Controller{
@RequestMapping("/find-by-phones")
public String find(List<String> phones){
...
}
}
Can Spring MVC some how convert multi-value param phones
to a list of String
s (or other objects?
Thanks.
Alex
"Arrays" in @RequestParam
are used for binding several parameters of the same name:
phone=val1&phone=val2&phone=val3
-
public String method(@RequestParam(value="phone") String[] phoneArray){
....
}
You can then convert it into a list using Arrays.asList(..)
method
EDIT1:
As suggested by emdadul, latest version of spring can do like below as well:
public String method(@RequestParam(value="phone", required=false) List<String> phones){
....
}
Spring can convert the query param directly into a List
or even a Set
for example:
@RequestParam(value = "phone", required = false) List<String> phones
or
@RequestParam(value = "phone", required = false) Set<String> phones