Binding a list in @RequestParam
Solution 1
Arrays in @RequestParam
are used for binding several parameters of the same name:
myparam=myValue1&myparam=myValue2&myparam=myValue3
If you need to bind @ModelAttribute
-style indexed parameters, I guess you need @ModelAttribute
anyway.
Solution 2
Or you could just do it that way:
public String controllerMethod(@RequestParam(value="myParam[]") String[] myParams){
....
}
That works for example for forms like this:
<input type="checkbox" name="myParam[]" value="myVal1" />
<input type="checkbox" name="myParam[]" value="myVal2" />
This is the simplest solution :)
Solution 3
Subscribing what basil said in a comment to the question itself, if method = RequestMethod.GET
you can use @RequestParam List<String> groupVal
.
Then calling the service with the list of params is as simple as:
API_URL?groupVal=kkk,ccc,mmm
Solution 4
Just complementing what Donal Fellows said, you can use List with @RequestParam
public String controllerMethod(@RequestParam(value="myParam") List<ObjectToParse> myParam){
....
}
Hope it helps!
Solution 5
One way you could accomplish this (in a hackish way) is to create a wrapper class for the List
. Like this:
class ListWrapper {
List<String> myList;
// getters and setters
}
Then your controller method signature would look like this:
public String controllerMethod(ListWrapper wrapper) {
....
}
No need to use the @RequestParam
or @ModelAttribute
annotation if the collection name you pass in the request matches the collection field name of the wrapper class, in my example your request parameters should look like this:
myList[0] : 'myValue1'
myList[1] : 'myValue2'
myList[2] : 'myValue3'
otherParam : 'otherValue'
anotherParam : 'anotherValue'
Related videos on Youtube
Javi
Updated on January 17, 2020Comments
-
Javi over 4 years
I'm sending some parameters from a form in this way:
myparam[0] : 'myValue1' myparam[1] : 'myValue2' myparam[2] : 'myValue3' otherParam : 'otherValue' anotherParam : 'anotherValue' ...
I know I can get all the params in the controller method by adding a parameter like
public String controllerMethod(@RequestParam Map<String, String> params){ .... }
I want to bind the parameters myParam[] (not the other ones) to a list or array (anything that keeps the index order), so I've tried with a syntax like:
public String controllerMethod(@RequestParam(value="myParam") List<String> myParams){ .... }
and
public String controllerMethod(@RequestParam(value="myParam") String[] myParams){ .... }
but none of them are binding the myParams. Even when I add a value to the map it is not able to bind the params:
public String controllerMethod(@RequestParam(value="myParam") Map<String, String> params){ .... }
Is there any syntax to bind some params to a list or array without having to create an object as @ModelAttribute with a list attribute in it?
Thanks
-
skaffman over 13 yearsI don't think this is possible. The code in
HandlerMethodInvoker.resolveRequestParam
only ever gets the first value -
basil almost 8 years(Spring Boot): Is about
method = RequestMethod.GET
ormethod = RequestMethod.POST
? If.GET
@RequestParam List<String> groupVal
fulfilled from?groupVal=kkk,ccc,mmm
successfully (Spring Boot)
-
-
Javi over 13 yearsthere may be problems with the order (which is very important to keep in my case) because I send the parameters by serializing a form and sending i with ajax. I'll use the "traditional" @ModelAttribute way.
-
Javi over 13 yearsWell this is almost the same as using @ModelAttribute, the only difference is that the param is not annotated. I wanted to avoid @ModelAttribute just because I didn't want to create a wrapper. I read somewhere in stackoverflow (I can't remember where exactly) that if you add a param in the controller method without @ModelAttribute annotation (and it wasn't a special object like HttpRequest, HttpResponse...) the framework treat it as if it were annotated with @ModelAttribute. So if that was true this is exactly as having @ModelAttribute. But thanks for your answer.
-
andrew cooke over 12 yearsdoes that preserve the order?
-
M Smith about 12 yearsI was able to use just the name rather than the [] in Spring 3.0 thusly: @RequestParam(value="myParam") String[] myParams
-
Pedro over 11 yearsI do not share the findings of @MSmith, though.
-
Juzer Ali about 11 yearsIs it possible to obtain
List<String>
through this. Also is it possible to obtain a java bean likeList<MyBean>
-
theblang over 10 yearsI think you can remove the brackets from param name.
-
yosh almost 10 yearsWorks without brackets, but they may be needed if you use jQuery to push each selected checkbox into array and send it as param.
-
Chomeh about 9 yearsWould you happen to know how to construct a URI with this sort mapping with UriTemplate, or some other means? (for a client of this sort of resource).
-
Chomeh about 9 yearsAnswering my own question, it apears the spring UriTemplate doesn't support RFC6570, use the damnhandy implementation: stackoverflow.com/questions/14153036/…
-
Ian Newland almost 8 yearsWith jQuery, I found that sending it as a comma separated string seemed to work:
var array = '1,5,33';
-
zygimantus about 7 yearsIt does not work with this kind of url:
&order[0][column]=1&order[0][dir]=desc
? -
Top Cat over 5 yearsUsing
myParam[]
saved me. Using justmyParam
didn't work. -
Jimmy lau over 2 yearsI have a small problem. What should i do if one of the string element contains ',' and the url decodes this as two separated string element.
-
James Gawron about 2 yearsWas this attempting to answer the OP at all ? Doesn't seem related.