Reputation: 15176
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¶m=value2¶m=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!
Upvotes: 41
Views: 95437
Reputation: 849
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 =>{
// ...
})
Upvotes: 0
Reputation: 723
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 []
)
Upvotes: 55
Reputation: 32535
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?
Upvotes: 5
Reputation: 136
@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.
Upvotes: 4
Reputation: 1045
This should work:
@RequestMapping(value="/schedule", method = RequestMethod.POST)
public void action(@RequestParam("param") String[] param)
Upvotes: 8
Reputation: 845
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();
...
}
Upvotes: 4