java - 发送Json数据到Spring Controller

标签 java angularjs json spring spring-mvc

我正在尝试将 json 数据从 Angular Post 发送到 Spring Controller ,但在控制台中收到 404 :

POST http://localhost:8080/app/orderDetails/saveOrder/[object%20Object],[object%20Object] 404 (Not Found)

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(Spring)

@RequestMapping(value = "/saveOrder/list",headers="Accept=application/json")

public @ResponseBody Object saveOrder(@PathVariable List list){
}   

最佳答案

您不能将List用作PathVariable。改为 RequestBody 并修复您的 JS 代码以将 $scope.list 作为正文发送,而不是将其附加到 url:

-$http.post("/app/orderDetails/saveOrder/"+$scope.list)
+$http.post("/app/orderDetails/saveOrder", $scope.list); 

还创建一个带有 idquantity 字段的包装对象,例如:

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){

关于java - 发送Json数据到Spring Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45613132/

相关文章:

java - 所有线程的compareAndSet 都会失败吗?

javascript - 众所周知,单例是不好的做法, Angular 是基于单例的。 Angular 不好吗?

python - 在python中递归地删除json对象列表中的null/空值

Go 中 url 的 JSON 解码

java - 具有多个参数的 JAX-RS Post 方法

java - 将 API 代码与类一起放入 JAR 中有什么缺点吗?

java - JVM cucumber : Running multiple testcase on a single step definition

javascript - Angular 1 : multiple conditions with multiple conditions OR how to exclude conditions if other conditions are true

javascript - 在 Google Drive 中生成第三方应用程序文件预览的最佳方法?

java - 你能将一个对象转换为具有特定的功能吗?