44

Suppose a hyperlink is clicked and an url is fired with the following parameter list myparam=myValue1&myparam=myValue2&myparam=myValue3 . Now how can I capture all the parameters using @RequestParam in spring mvc?

My requirement is I have to capture all the params and put them in a map.

Please help!

Shivayan Mukherjee
  • 766
  • 3
  • 10
  • 33

9 Answers9

45
@RequestMapping(value = "users/newuser", method = RequestMethod.POST)   
public String saveUser(@RequestParam Map<String,String> requestParams) throws Exception{
   String userName=requestParams.get("email");
   String password=requestParams.get("password");

   //perform DB operations

   return "profile";
}

You could use RequestParam in the above mentioned manner.

Touchstone
  • 5,575
  • 7
  • 41
  • 48
31

It seems you can't get

Map<String,String>

because all your params have same name "myparam"

Try this instead:

public ModelAndView method(@RequestParam("myparam") List<String> params) { }
barbiepylon
  • 891
  • 7
  • 23
Vitalii Velikodnyi
  • 1,312
  • 11
  • 18
  • 3
    It should be '@RequestParam' rather than '@RequestRaram'. Notice the 'P' rather than 'R'. I tried using the above code and got an error and didn't catch it right away because of this subtle misspelling. Hope this helps someone. Thanks for the useful answer :) – JulianDavid Oct 01 '15 at 18:55
18

To get all parameters at once try this:

public ModelAndView postResultPage(@RequestParam MultiValueMap<String, String> params)

This feature is described in the @RequestParam java doc (3. Paragraph):

Annotation which indicates that a method parameter should be bound to a web request parameter. Supported for annotated handler methods in Servlet and Portlet environments.

If the method parameter type is Map and a request parameter name is specified, then the request parameter value is converted to a Map assuming an appropriate conversion strategy is available.

If the method parameter is Map<String, String> or MultiValueMap<String, String> and a parameter name is not specified, then the map parameter is populated with all request parameter names and values.

Ralph
  • 118,862
  • 56
  • 287
  • 383
8

As of Spring 3.0, you can also use MultiValueMap to achieve this:

A rudimentary example would be:

public String someMethod(@RequestParam MultiValueMap<String,String> params) {

    final Iterator<Entry<String, List<String>>> it = params.entrySet().iterator();

    while(it.hasNext()) {
        final String k = it.next().getKey();
        final List<String> values = it.next().getValue();
    }

    return "dummy_response";

}
Brian
  • 4,921
  • 3
  • 20
  • 29
3

If anyone is trying to do the same in Spring Boot, use RequestBody in place of RequestParam

Akki
  • 754
  • 7
  • 22
3

Spring mvc can support List<Object>, Set<Object> and Map<Object> param, but without @RequestParam.

Take List<Object> as example, if your object is User.java, and it like this:

public class User {
    private String name;
    private int age;

    // getter and setter
}

And you want pass a param of List<User>, you can use url like this

http://127.0.0.1:8080/list?users[0].name=Alice&users[0].age=26&users[1].name=Bob&users[1].age=16

Remember to encode the url, the url after encoded is like this:

http://127.0.0.1:8080/list?users%5B0%5D.name=Alice&users%5B0%5D.age=26&users%5B1%5D.name=Bob&users%5B1%5D.age=16

Example of List<Object>, Set<Object> and Map<Object> is displayed in my github.

Pablo
  • 1,604
  • 16
  • 31
Frank.Chang
  • 883
  • 1
  • 10
  • 10
0

You can use for multiple Params as such

public String saveUser(@RequestParam("email") String userName, @RequestParam("password") String password) throws Exception{
   //your code
   //perform DB operations

   return "profile";
}
Amir Sidik
  • 11
  • 5
0

For params with same name, you can use MultiValueMap<String ,String>. Then all the values would be present as List

-2

You can use multiple @RequestParam annotations as shown below.

@RequestParam(value="myparam1", required = true) <Datatype> myparam1,
@RequestParam(value = "myparam2", required = false) <Datatype> myparam2,
wyse12
  • 7
  • 4