Hello Friends i am working on this problem from 2 days and not been able to solve. i Hope that stackoverflow could help me.
Problem: I have sent JSON object through ajax, at backend i have a class(named: SalesCommandObject) containing objects of other models, getters and setters of these. then trying to send an JSON object of type "SalesCommandObject" to controller method. so that the json data can be mapped into model data.
But the server throws error: "400 Bad Request :The request sent by the client was syntactically incorrect".
I am posting the entire code. Please Check and Help Me out.
// Code for Ajax POST:
var salesCommandObject = {};
salesCommandObject.CustomerInfo =
{
"address1": "Address_1",
"city": "City",
"pin": "PIN"
};
salesCommandObject.SalesModel =
{
"locality":'Loc1',
"shippingType":'Regular',
"shippingCost":20
};
$.ajax
({
type: "POST",
dataType : 'json',
async : true,
url: "http://localhost:8080/OnlineStore/kmsg/grocery/SaveSalesOrder",
data : JSON.stringify(salesCommandObject),
contentType: "application/json; charset=utf-8"
}).done(function(data,type,xml)
{
alert("result");
console.log(data);
}).fail(function()
{
alert("Something Bad Happened, Service failed");
})
// Code of Controller Receiving JSON Object:
@RequestMapping(value = "/SaveSalesOrder", method = RequestMethod.POST, consumes=MediaType.APPLICATION_JSON_VALUE)
public @ResponseBody String SaveCustomerOrder(@RequestBody SalesCommandObject salesCommandObject) throws Exception
{
CustomerModel cust = salesCommandObject.getCustomerInfo();
SalesModel sale = salesCommandObject.getLocality();
System.out.println(cust.getAddress1());
System.out.println(sale.getLocality());
return "Success";
}
//Code of class Model salesCommandObject
public class SalesCommandObject
{
private CustomerModel CustomerInfo = new CustomerModel();
private List<SalesItemsModel> salesData ;
private SalesModel salesModel = new SalesModel();
private SalesDeliverySlotsModel salesDelSlotsModel = new SalesDeliverySlotsModel();
private List<ItemsForSaleModel> itemsforSale ;
// getters and setters here//
}