[英]Receiving POST data with Java Spring
我正在尝试使用以下代码捕获已发布到Java控制器的数组:
@RequestMapping(method=RequestMethod.POST, value="/json/foo.json")
public @ResponseBody Object foo(List<Integer> fooIds)
{
for (Integer id : fooIds)
{
fooService.delete(id);
}
return null;
}
但是我一直收到以下错误:
Could not instantiate bean class [java.util.List]: Specified class is an interface
我发布的数组设置如下(在PHP中):
$array = array(
"fooIds[0]" => 1,
"fooIds[1]" => 2,
"fooIds[2]" => 3,
"fooIds[3]" => 4,
"fooIds[4]" => 5,
);
最初我尝试过:
$array = array(1,2,3,4,5);
但这也不起作用。
我认为代码中的唯一问题是您应该使用@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;
}
尝试使用ArrayList而不是List。 许多工具都存在类似的问题,因此,当期望使用List时,它们不知道要实例化哪个类。
指定所需数组的具体实现,例如public @ResponseBody Object foo(ArrayList<Integer> fooIds)
或定义一个转换器。
尝试将您的方法签名更改为public @ResponseBody Object foo(int[] fooIds)
Could not instantiate bean class [java.util.List]: Specified class is an interface.
指示您应使用实现List的类,例如ArrayList
尝试写作
new List<Object>() // the compiler will complain
new ArrayList<Object>() // the compiler will not complain
我已经可以使用以下代码:
@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;
}
然后按以下方式设置阵列:
$array = array(
'fooIds' => '1,2,3,4,5',
);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.