0

I am trying to send json data from angular post to spring controller, but getting 404 in console :

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){
}   
Pratyush Pranjal
  • 544
  • 6
  • 26

2 Answers2

4

You can't use List as PathVariable. Make it RequestBody instead and fix your JS code to send $scope.list as a body, not append it to url:

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

Also create a wrapper object with id and quantity fields, something like:

public class CartItem {
    private Long id;
    private Long quantity;
    //getters and setters
}

Then you'll be able to parameterize your List with this object:

- public @ResponseBody Object saveOrder(@PathVariable List list){
+ public @ResponseBody Object saveOrder(@RequestBody List<CartItem> list){
Bohdan Levchenko
  • 3,411
  • 2
  • 24
  • 28
  • 1
    Just a suggestion, but I would also add that `@PathVariable` is looking/for/a/path vs an object in the body – Brian Aug 10 '17 at 12:14
  • @Brian yeah it is a good idea if the same method is used for both 'POST' and 'PUT' operations. It is mapped for all operations but something tells me that it is used for 'POST' only. – Bohdan Levchenko Aug 10 '17 at 12:28
3

You should replace @PathVariable with @Request Body and fix your js code to send list as part of the request body (instead of adding it to the path).

albert_nil
  • 1,648
  • 7
  • 9