0

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//
}

1 Answer 1

0

Friends i have solved this problem:

I haven't declared the default constructors in the Models that's why type of data i was sending through ajax was not matching the data type in controller parameter at receiving end. Also the jackson version in pom.xml not set correctly. Few things that must be kept in mind is: The key value of the Json Objects must match the set method name: For example if: Json data is {"key1":"value","keyTwo":"value123"} then setters:

setKey1(){} setKeyTwo(){}

Also there must be public default constructor in all the used model classes, in which we are going to map our json data.

Thank you.

Sign up to request clarification or add additional context in comments.

3 Comments

How about the data "locality":'Loc1' does it get mapped ?
Yes my friend single quote or double quote won't bother json too much both will be taken as String data if the data is a variable then u don't have to give any quote.
Can you post your model class details.CustomerInfo and SalesModel

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.