-1

This is my anular js to pass json to spring controller

$scope.submit = function() {

  $scope.json = JSON.parse(localStorage
    .getItem("itemDes"));

  var jsonlen = Object.keys($scope.json).length;
  for (var i = 0; i < jsonlen; i++) {
    var qnty = document
      .getElementById("qunty_" + i).value;
    // alert(qnty);
    var price = document.getElementById("subtot_" + i).value;

    $scope.object2 = {
      totquanty: qnty,
      totprice: price
    };
    angular.extend($scope.json[i], $scope.object2);

  }
  var config = {
    headers: {
      'Content-Type': 'application/json;charset=utf-8;'
    }
  }
  alert(JSON.stringify($scope.json));
  //localStorage.clear();
  var response = $http.post('${pageContext.request.contextPath}/saveCartOrder',
    "hiii");


  response.success(function(data, status, headers, config) {
    alert(data);
  });
  response.error(function(data, status, headers, config) {
    alert("Exception details: " + JSON.stringify({
      data: data
    }));
  });

};

This is my spring controller:

@RequestMapping(value = "/saveCartOrder", method = RequestMethod.POST, headers = {
  "Accept=*/*", "Content-Type=application/json"
})
public void saveCartOrder(@RequestBody CartBean cart) {
  System.out.println(cart.getItemname());

}

I have added the jackson jar to the project: jackson-annotations-2.8.0.jar jackson-core-2.8.6.jar jackson-databind-2.8.6.jar jackson-mapper-asl-1.9.13.jar

Still iam getting this 415 Unsupported Media Type error.

Abhishek Pandey
  • 13,302
  • 8
  • 38
  • 68

1 Answers1

0

Remove headers and add consumes=MediaType.APPLICATION_JSON_UTF8_VALUE as you are setting it to 'Content-Type': 'application/json;charset=utf-8;'

@RequestMapping(value = "/saveCartOrder", method = RequestMethod.POST, consumes=MediaType.APPLICATION_JSON_UTF8_VALUE )
public void saveCartOrder(@RequestBody CartBean cart) {
  System.out.println(cart.getItemname());

}

Doc: http://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/http/MediaType.html#APPLICATION_JSON_UTF8

Secondly, ensure @requestBody Cartbean is satisfied by looking at the json sent as part of the request . if it doesn`t mataches, it will not work as expected

amicoderozer
  • 2,046
  • 6
  • 28
  • 44
Barath
  • 5,093
  • 1
  • 17
  • 42
  • I have added your code and import org.springframework.http.MediaType;.But getting Type mismatch: cannot convert from MediaType to String[] error – suman biswas Jan 27 '17 at 07:47
  • This is my json data:[{"id":65,"itemname":"itemtemp","price":100,"discount":0,"qunty":12,"totquanty":"1","totprice":"100"}] And My CartBean :private int id; private String itemname; private float price; private float discount; private Integer qunty; private String totquanty; private String totprice; – suman biswas Jan 27 '17 at 07:54
  • @sumanbiswas use consume=MediaType.APPLICATION_JSON_UTF8_VALUE instead. It return a String and it's the correct value. – amicoderozer Jan 27 '17 at 08:09
  • @amicoderozer still same error.i m not getting where i m doing the wrong. – suman biswas Jan 27 '17 at 09:01