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!

1
  • Are all your parameters called "myparam"? Commented Mar 14, 2014 at 8:20

9 Answers 9

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.

Sign up to request clarification or add additional context in comments.

3 Comments

How do I make a soapui rest test for this type of map object?
What if the RequestParams is different data types?
This doesn't answer the question. The question is asking about parameters that have multiple values for the same name.
35

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) { }

1 Comment

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 :)
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.

2 Comments

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..
this is best solution if you have several params with same name
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";

}

1 Comment

Do you know if Swagger is supporting this option?
3

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

Comments

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.

3 Comments

"but without @RequestParam" this was the key, tks.
@Frank.Chang where exactly in your Github ? The link does not show what you said...
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";
}

1 Comment

This is not the answer of the question.
0

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

Comments

-2

You can use multiple @RequestParam annotations as shown below.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.