41

I was just wondering how to pass in post parameters such as following exercept from html options, normally i would get a array in language such as php (POST['param'][0]... would work i believe)

url?param=value1&param=value2&param=value3

I tried:

@RequestMapping(value="/schedule", method = RequestMethod.POST)
public void action(String[] param)

But this doesn't work for some reason...

Any ideas would be greatly appreciated!

4
  • I think you want to add @RequestParam to your method param Commented Mar 23, 2011 at 0:30
  • Does any of the proposed solutions work? Commented Nov 12, 2013 at 13:49
  • Hi, even I am facing the same problem now. So it is better to use -> (@RequestParam(value = "param[]") String[] paramValues) "OR" (@RequestParam MultiValueMap<String, String> params) . I am confused. Can you please help me? Commented Jul 1, 2015 at 8:03
  • I think best syntax is param[]=value1&param[]=value2&... because multiple simple (without special postfix like "[]") params may be considered as program error. Can Spring MVC parse this into array? Commented Aug 23, 2017 at 18:20

6 Answers 6

55

You can use this:

@RequestMapping(value="/schedule", method = RequestMethod.POST)
public void action(@RequestParam(value = "param[]") String[] paramValues){...}

it will retrieve all values (inside array paramValues) of parameter param (note the attribute value of RequestParam: it ends with [])

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

5 Comments

By the way, when you send ajax param in jquery, don't forget to make the array param name also end with "[]".
Does this work with an integer or long array? So far, I can only make this work with strings.
@kaleemsagard Hi, even I am facing the same problem now. So it is better to use -> (@RequestParam(value = "param[]") String[] paramValues) "OR" (@RequestParam MultiValueMap<String, String> params) . I am confused. Can you please help me?
@Chandz I think that @RequestParam MultiValueMap<String, String> params retrieves all the parameters in the request, not only the values of the parameter param that are needed, so you can use the MultiValueMap object and search in it all the ocurrences of the parameter param. That could take harder work than @RequestParam(value = "param[]").
The only right solution that I have found on the internet. Thanks a lot!
8

This should work:

@RequestMapping(value="/schedule", method = RequestMethod.POST)
public void action(@RequestParam("param") String[] param)

2 Comments

This does work but is slightly different from what the question asks - this will map a SINGLE parameter that has an array as a value; however MULTIPLE parameters (presumably with dynamic and hard to anticipate names) cannot be mapped like this.
@Eugen: Hi, even I am facing the same problem now. So it is better to use -> (@RequestParam(value = "param[]") String[] paramValues) "OR" (@RequestParam MultiValueMap<String, String> params) . I am confused. Can you please help me?
5

If anyone is still struggling with that, this is how it should be done:

Form inputs:

<input name="myParam" value="1"/>
<input name="myParam" value="4"/>
<input name="myParam" value="19"/>

Controller method:

   @RequestMapping
    public String deletePlaces(@RequestParam("myParam") Long[] myParams) {

      //myParams will have 3 elements with values 1,4 and 19
    }

This works the same for String[] Integer[] Long[] and probably more. POST,GET,DELETE will work the same way.

Parameter name must match name tag from form input. No extra [] needed etc. In fact, param name can be ommited, if method argument name is the same as input name so we can end up with method signature like this:

   @RequestMapping
   public String deletePlaces(@RequestParam Long[] myParams) 

and it will still work

Something extra: Now if you have domain model lets say Place and you have PlaceRepository, by providing Place#id as value of your inputs, Spring can lookup related objects for us. So if we assume that form inputs above contais User ids as values, then we can simply write this into controller:

public String deletePlaces(@RequestParam Place[] places) {
  //places will be populated with related entries from database!
 }

Sweet isn't it?

Comments

4

If you know your param name, try

@RequestMapping(value="/schedule", method = RequestMethod.POST)
public void action(@RequestParam("myParam") String param)

Another way is to use the current request object:

@RequestMapping(value="/schedule", method = RequestMethod.POST)
public void action(HttpServletRequest request) {
Map parameterMap = request.getParameterMap();
...

}

Comments

4
@RequestMapping(value="/schedule", method = RequestMethod.POST)
public void action(@RequestParam(value = "param[]") String[] paramValues {...}

This will work when you send for example ajax by jQuery:

$.ajax({
    type: "POST",
    data: { param:paramValues }
    ...
});

but when you send only single element array with String, which contains commas like "foo,baa", Spring will split this String and create array with 2 elements "foo" and "baa". So be careful with commas when your array can have also one element.

Comments

0

1, add a java class as requestBody

public class PostMembers {
    private String[] members;

    public String[] getMembers() {
        return members;
    }

    public void setMembers(String[] members) {
        this.members = members;
    }
}

2, add a method in controller class

@PostMapping("")
public List<SomeClass> addMember(@RequestBody PostMembers postMembers) {
// ...
}

now your controller can get array params .

3, test with curl

curl -H "Content-Type:application/json"  \
 -X POST -d '{"members":["item1","item2"]}' \
 http://localhost:8080/api/xxx

or if you use vue

axios.post(someUrl,{
    members:[
       'item1','item2'
    ]
}).then(response =>{
// ...
})

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.