Binding a list in @RequestParam

272,013

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'
Share:
272,013

Related videos on Youtube

Javi
Author by

Javi

Updated on January 17, 2020

Comments

  • Javi
    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
      skaffman over 13 years
      I don't think this is possible. The code in HandlerMethodInvoker.resolveRequestParam only ever gets the first value
    • basil
      basil almost 8 years
      (Spring Boot): Is about method = RequestMethod.GET or method = RequestMethod.POST? If .GET @RequestParam List<String> groupVal fulfilled from ?groupVal=kkk,ccc,mmm successfully (Spring Boot)
  • Javi
    Javi over 13 years
    there 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
    Javi over 13 years
    Well 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
    andrew cooke over 12 years
    does that preserve the order?
  • M Smith
    M Smith about 12 years
    I was able to use just the name rather than the [] in Spring 3.0 thusly: @RequestParam(value="myParam") String[] myParams
  • Pedro
    Pedro over 11 years
    I do not share the findings of @MSmith, though.
  • Juzer Ali
    Juzer Ali about 11 years
    Is it possible to obtain List<String> through this. Also is it possible to obtain a java bean like List<MyBean>
  • theblang
    theblang over 10 years
    I think you can remove the brackets from param name.
  • yosh
    yosh almost 10 years
    Works without brackets, but they may be needed if you use jQuery to push each selected checkbox into array and send it as param.
  • Chomeh
    Chomeh about 9 years
    Would 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
    Chomeh about 9 years
    Answering my own question, it apears the spring UriTemplate doesn't support RFC6570, use the damnhandy implementation: stackoverflow.com/questions/14153036/…
  • Ian Newland
    Ian Newland almost 8 years
    With jQuery, I found that sending it as a comma separated string seemed to work: var array = '1,5,33';
  • zygimantus
    zygimantus about 7 years
    It does not work with this kind of url: &order[0][column]=1&order[0][dir]=desc?
  • Top Cat
    Top Cat over 5 years
    Using myParam[] saved me. Using just myParam didn't work.
  • Jimmy lau
    Jimmy lau over 2 years
    I 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
    James Gawron about 2 years
    Was this attempting to answer the OP at all ? Doesn't seem related.