![](/img/trans.png)
[英]JQuery datatable passing JSON to Spring controller, got error 400 Bad Request
[英]400 (Bad Request) on passing JSON to Spring controller
我试图将对象数组作为JSON字符串传递给Spring控制器。 我的JSON格式的数据看起来像
[{
"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");
}
});
为此,我有两个bean OuterCover
和CustInfo
。 当OuterCover
具有的清单CustInfo
和CustInfo
有id
和name
。
public class OuterCover {
List<CustInfo> myCustInfoList;
//getter & setter
}
但是我得到了400(错误请求)。 有什么建议吗?
您需要将OuterCover类封装到其他类中,因为请求具有OuterCover元素。 例如
class RequestDto{
@JsonElement("outerCover")
private OuterCover outerCover;
//getters and setters
}
或者,您可以修改请求有效负载并删除“ outerCover”元素,例如:
[{
"id": 123456,
"name": "First Item"
},
{
"id": 78910,
"name": "Second Item"
}]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.