2015-03-25 89 views
2

我向控制器發送以下JSON並嘗試訪問它,但我得到「客戶端發送的請求在語法上不正確。」錯誤信息。Spring中的@RequestParam中的JSON數組MVC

JSON:

{ 
"employees": [ 
{ 
    "firstName": "John", 
    "lastName": "Doe" 
}, 
{ 
    "firstName": "Anna", 
    "lastName": "Smith" 
}, 
{ 
    "firstName": "Peter", 
    "lastName": "Jones" 
} 
]} 

控制器:

@RequestMapping(value="/xyz",produces="application/json",consumes="application/json",method=RequestMethod.POST) 
public String sendEmpDetails(@RequestBody List<Employee> employeeList){ 
    return "xyz"; 
} 

我到底做錯了什麼?

+0

這個鏈接可以幫助你。 http://stackoverflow.com/questions/14208682/the-request-sent-by-the-client-was-syntactically-incorrect-spring-restclie – lateralus 2015-03-25 08:05:33

回答

0

您使用的是複雜的對象,但只需要列出的陣列,因此,建立你的JSON這樣的:

var items = []; 
    { 
     var item = {}; 
     item ["firstName"] = "John"; 
     item ["lastName"] = "Doe"; 
     items.push(item); 
    } 
    { 
     var item = {}; 
     item ["firstName"] = "Anna"; 
     item ["lastName"] = "Smith"; 
     items.push(item); 
    } 
    { 
     var item = {}; 
     item ["firstName"] = "Peter"; 
     item ["lastName"] = "Jones"; 
     items.push(item); 
    } 
    $.ajax({ 
     url: '/myurl', 
     data: JSON.stringify(items), 
     type: 'POST', 
     dataType: 'json', 
     contentType: "application/json; charset=utf-8", 
     success: function (response) { 
      alert(response); 
     }, 
     error: function (xhr, status, errorThrown) { 
      alert(errorThrown); 
     } 
    } 
相關問題