我正在尝试将json数据从angular post发送到spring控制器,但在控制台中获取404:
POST http://localhost:8080/app/orderDetails/saveOrder/[object%20Object],[object%20Object] 404(未找到)
JS
var i=0;
var j = ngCart.getCart().items.length;
$scope.list=[];
for(i;i<j;i++){
$scope.list.push({
'id': ngCart.getCart().items[i]._id,
'quantity':ngCart.getCart().items[i]._quantity
});
}
$http.post("/app/orderDetails/saveOrder/"+$scope.list)
的Java(春)
@RequestMapping(value = "/saveOrder/list",headers="Accept=application/json")
public @ResponseBody Object saveOrder(@PathVariable List list){
}
答案 0 :(得分:4)
您无法将List
用作PathVariable
。改为使用RequestBody
并修复您的JS代码,将$scope.list
作为正文发送,而不是将其附加到网址:
-$http.post("/app/orderDetails/saveOrder/"+$scope.list)
+$http.post("/app/orderDetails/saveOrder", $scope.list);
还要创建一个包含id
和quantity
字段的包装器对象,如:
public class CartItem {
private Long id;
private Long quantity;
//getters and setters
}
然后,您将能够使用此对象参数化List
:
- public @ResponseBody Object saveOrder(@PathVariable List list){
+ public @ResponseBody Object saveOrder(@RequestBody List<CartItem> list){
答案 1 :(得分:3)
您应该将@PathVariable替换为@Request Body并修复您的js代码以将列表作为请求正文的一部分发送(而不是将其添加到路径中)。