how to capture multiple parameters using @RequestParam using spring mvc?
Asked Answered
P

9

48

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!

Peso answered 14/3, 2014 at 7:39 Comment(1)
Are all your parameters called "myparam"?Unmeant
A
47
@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.

Ascarid answered 14/3, 2014 at 9:17 Comment(3)
How do I make a soapui rest test for this type of map object?Uncle
What if the RequestParams is different data types?Shirtwaist
This doesn't answer the question. The question is asking about parameters that have multiple values for the same name.Guerdon
R
34

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) { }
Rasure answered 14/3, 2014 at 8:6 Comment(1)
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 :)Steapsin
K
21

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.

Kiwanis answered 14/3, 2014 at 7:52 Comment(2)
Okay i got it.. I am able to get all the parameters and their values in params,now how can i use them? please show me that..Peso
this is best solution if you have several params with same nameFrankel
D
9

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";

}
Doggo answered 1/9, 2017 at 3:50 Comment(1)
Do you know if Swagger is supporting this option?Jokjakarta
L
3

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

Lesleylesli answered 23/4, 2019 at 20:23 Comment(0)
H
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.

Horizon answered 24/4, 2019 at 9:52 Comment(3)
"but without @RequestParam" this was the key, tks.Nicholas
@Horizon where exactly in your Github ? The link does not show what you said...Sissy
@ jozinho22 I mean this: github.com/Flamingo93/spring-mvc-test/blob/master/src/main/java/…Horizon
B
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";
}
Battologize answered 30/1, 2022 at 7:18 Comment(1)
This is not the answer of the question.Apeman
S
0

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

Stat answered 17/3, 2022 at 6:31 Comment(0)
M
-2

You can use multiple @RequestParam annotations as shown below.

@RequestParam(value="myparam1", required = true) <Datatype> myparam1,
@RequestParam(value = "myparam2", required = false) <Datatype> myparam2,
Milkwhite answered 1/7, 2021 at 16:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.