用JavaSpring接收POST数据
我正在尝试捕获已发布到Java控制器的数组,代码如下所示:用JavaSpring接收POST数据,java,arrays,spring,list,post,Java,Arrays,Spring,List,Post,我正在尝试捕获已发布到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;
@RequestMapping(method=RequestMethod.POST, value="/json/foo.json")
public @ResponseBody Object foo(List<Integer> fooIds)
{
for (Integer id : fooIds)
{
fooService.delete(id);
}
return null;
}
我发布的数组设置如下(在PHP中):
最初我尝试:
$array = array(1,2,3,4,5);
但这也没用。试试ArrayList而不是List。许多工具都有类似的问题,所以当需要列表时,它们不知道实例化哪个类。指定所需数组的具体实现,如
public@ResponseBody对象foo(ArrayList fooIds)
或定义转换器。尝试将方法签名更改为public@ResponseBody对象foo(int[]fooIds)
指示应使用类实现列表,例如ArrayList
试着写
new List<Object>() // the compiler will complain
new ArrayList<Object>() // the compiler will not complain
newlist()//编译器会抱怨
new ArrayList()//编译器不会抱怨
我可以使用以下代码:
@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',
);
我认为代码中唯一的问题是应该用@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;
}
@RequestMapping(method=RequestMethod.POST,value=“/json/foo.json”)
public@ResponseBody对象foo(@RequestParam(“ids”)列表fooIds)
{
for(整数id:fooIds)
{
fooService.delete(id);
}
返回null;
}
这给了我一个ArrayList变量,但它仍然是空的。使用ArrayList会停止错误…但数组本身仍然是空的。不要将标题更改为[已解决]
,接受答案将其标记为已解决。我无法接受我自己的答案两天……因此我将其标记为已解决,以进行临时测量。
@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',
);
@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;
}