请求长的参数列表

时间:2014-11-14 17:40:51

标签: jquery spring-mvc

eventIdsToDelete [“24616342”,“24615878”]我正在尝试将Long值列表发送到Spring控制器,但是我收到了400 Bad Request错误。所以我猜我的请求映射签名是不正确的。

我的jQuery AJAX调用

var myList = [24616342,24616201,24616310];
$.ajax({
  url: '/myApp/path/toController',
  type: 'POST',
  data: {myList: JSON.stringify(myList)},
  success: function(response) { ... }
}); 

我的请求映射

@RequestMapping(value = "/myApp/path/toController", method = RequestMethod.POST)
public @ResponseBody boolean doSomething(Model model, @RequestParam List<Long> myList)   
{
    System.out.println(myList);
    return true;
}

在firebug控制台中我看到URL被调用但在post标签中我看到参数为

myList ["24616342","24615878"]

我尝试将请求映射参数更改为

List<String> 

它工作正常。但我希望请求映射方法类型为String。

2 个答案:

答案 0 :(得分:4)

解决了以下变化

(a)jQuery Call

 data: {myList: myList},

(b)控制器方法

@RequestMapping(value = "/myApp/path/toController", method = RequestMethod.POST)
public @ResponseBody boolean doSomething(Model model, @RequestParam("myList[]") List<Long> myList)   
{
   System.out.println(myList);
   return true;
}

答案 1 :(得分:0)

尝试使用@RequestBody

@RequestMapping(value = "/myApp/path/toController", method = RequestMethod.POST)
public @ResponseBody boolean doSomething(Model model, @RequestBody List<Long> data)   
{
    System.out.println(data);
    return true;
}