0
我想將對象數組作爲JSON字符串傳遞給Spring控制器。我的JSON格式的數據看起來像關於將JSON傳遞給Spring控制器的400(錯誤請求)
[{
"id": 123456,
"name": "First Item"
},
{
"id": 78910,
"name": "Second Item"
}]
所以我發送到控制器
@RequestMapping(value = "/some/url", method = RequestMethod.POST, consumes = "application/json")
public void doSomething(@RequestBody List<CustInfo> myCustInfoList) {
System.out.println("Message Received " + myCustInfoList);
}
這個AJAX調用
$.ajax({
type: 'POST',
url: '/some/url',
contentType: 'application/json',
data: '[{"id": 123456, "name": "First Item"}, {"id": 78910, "name": "Second Item"}]',
success: function() {
consloe.log("Success");
}
});
爲此,我有兩個豆OuterCover
和CustInfo
。其中OuterCover
具有CustInfo
和CustInfo
的列表具有id
和name
。
public class OuterCover {
List<CustInfo> myCustInfoList;
//getter & setter
}
但我得到400(壞請求)在這個。任何建議?
如果我想發送的'數據:{[{ 「ID」:123456,「名「:」First Item「},{」id「:78910,」name「:」Second Item「}]}' –
這將起作用,我在答案中增加了另一個解決方案。 –
我不能在控制器中使用'OutCover'嗎?我想避免需要創建一個類('RequestDto')。 –