Receiving POST data with Java Spring
I'm trying to catch an array that has been posted to my Java Controller with the code shown below:
@RequestMapping(method=RequestMethod.POST, value="/json/foo.json")
public @ResponseBody Object foo(List<Integer> fooIds)
{
for (Integer id : fooIds)
{
fooService.delete(id);
}
return null;
}
However I keep getting the following error:
Could not instantiate bean class [java.util.List]: Specified class is an interface
The array I am posting is s开发者_高级运维etup as follows (in PHP):
$array = array(
"fooIds[0]" => 1,
"fooIds[1]" => 2,
"fooIds[2]" => 3,
"fooIds[3]" => 4,
"fooIds[4]" => 5,
);
Originally I tried:
$array = array(1,2,3,4,5);
but that didn't work either.
Try ArrayList instead of List. Many tools have problems like that, so that they do not know what class to instantiate, when List is expected.
Specify a concrete implementation of an array you want, like public @ResponseBody Object foo(ArrayList<Integer> fooIds)
or define a converter.
Try to change your method signature to public @ResponseBody Object foo(int[] fooIds)
Could not instantiate bean class [java.util.List]: Specified class is an interface.
Indicates you should use an class implementing List for example ArrayList
try writing
new List<Object>() // the compiler will complain
new ArrayList<Object>() // the compiler will not complain
I've got it to working with the following code:
@RequestMapping(method=RequestMethod.POST, value="/json/foo.json")
public @ResponseBody Object foo(@RequestParam("ids") int[] fooIds)
{
for (Integer id : fooIds)
{
fooService.delete(id);
}
return null;
}
The array is then setup like this:
$array = array(
'fooIds' => '1,2,3,4,5',
);
I think the only problem in your code is that you should annotate the List with @RequestParam
@RequestMapping(method=RequestMethod.POST, value="/json/foo.json")
public @ResponseBody Object foo(@RequestParam("ids") List<Integer> fooIds)
{
for (Integer id : fooIds)
{
fooService.delete(id);
}
return null;
}
精彩评论