javascript - 400 Error When Sending JSON to Controller -


i try send json object javascript controller. 400 status code when sending object. tried many methods, last approach below. miss something? why keep getting 400?

ajax post call:

$.ajax({   type: "post",   url: "http://localhost:8080/services/savereservation",   cache: false,   data: savereservation,   contenttype: "application/json; charset=utf-8",   datatype: "json",    success: function(data) {     console.debug("after success");   } }); 

my object looks this:

{      "accommodaion":"a0d8185c-a238-5qe7-aa48-5c196b108aba",    "totaloutputprice":90,    "bookerinfo":{         "country":"xa",       "homeaddress":"",       "phonenumber":"0019382663773",       "contactchoice":"phone",       "name":"test name",       "id":"87"    },    "creditcard":{         "holdername":"holder test",       "cardtype":"discover",       "cardnumber":"6011303031648258",       "expmonth":"01",       "expyear":"2017",       "cvc":"123"    },    "checkin":"2016-11-16 06:43:19.77",    "checkout":"2017-03-16 06:43:19.77",    "totaltax":4,    "totalvat":13,    "roomoutputprice":"77" } 

controller:

@requestmapping(value = "/savereservation", method = requestmethod.post) public test savereservation(   @requestbody test savereservation) {   system.out.println(savereservation.getaccommodaion());   system.out.println(savereservation.getbookerinfo().getname());   system.out.println(savereservation);   return savereservation; } 

classes:

private class bookerinfo {     private string country;     private string homeaddress;     private string phonenumber;     private string contactchoice;     private string name;     private string id;     //getters setters } private class creditcard {     private string holdername;     private string cardtype;     private string expmonth;     private string expyear;     private string cvc;     //getters setters } private class test {     private string accommodaion;     private float totaloutputprice;     private bookerinfo bookerinfo;     private creditcard creditcard;     private string checkin;     private string checkout;     private float totaltax;     private float totalvat;     private float roomoutputprice;     //getters setters } 

error in framework:

16:12:14,096 debug exceptionhandlerexceptionresolver:134 - resolving exception handler [public ba.projectservice.controller.test ba.projectservice.controller.g2bcontroller.savereservation(ba.projectservice.controller.test)]: org.springframework.http.converter.httpmessagenotreadableexception: not read json: unrecognized field "cardnumber" (class ba.projectservice.controller.creditcard), not marked ignorable (5 known properties: , "cvc", "expmonth", "holdername", "cardtype", "expyear"])  @ [source: org.apache.catalina.connector.coyoteinputstream@793ea01d; line: 1, column: 287] (through reference chain: ba.projectservice.controller.test["creditcard"]->ba.projectservice.controller.creditcard["cardnumber"]); nested exception com.fasterxml.jackson.databind.exc.unrecognizedpropertyexception: unrecognized field "cardnumber" (class ba.projectservice.controller.creditcard), not marked ignorable (5 known properties: , "cvc", "expmonth", "holdername", "cardtype", "expyear"])  @ [source: org.apache.catalina.connector.coyoteinputstream@793ea01d; line: 1, column: 287] (through reference chain: ba.projectservice.controller.test["creditcard"]->ba.projectservice.controller.creditcard["cardnumber"]) 16:12:14,099 debug responsestatusexceptionresolver:134 - resolving exception handler [public ba.projectservice.controller.test ba.projectservice.controller.g2bcontroller.savereservation(ba.projectservice.controller.test)]: org.springframework.http.converter.httpmessagenotreadableexception: not read json: unrecognized field "cardnumber" (class ba.projectservice.controller.creditcard), not marked ignorable (5 known properties: , "cvc", "expmonth", "holdername", "cardtype", "expyear"])  @ [source: org.apache.catalina.connector.coyoteinputstream@793ea01d; line: 1, column: 287] (through reference chain: ba.projectservice.controller.test["creditcard"]->ba.projectservice.controller.creditcard["cardnumber"]); nested exception com.fasterxml.jackson.databind.exc.unrecognizedpropertyexception: unrecognized field "cardnumber" (class ba.projectservice.controller.creditcard), not marked ignorable (5 known properties: , "cvc", "expmonth", "holdername", "cardtype", "expyear"])  @ [source: org.apache.catalina.connector.coyoteinputstream@793ea01d; line: 1, column: 287] (through reference chain: ba.projectservice.controller.test["creditcard"]->ba.projectservice.controller.creditcard["cardnumber"]) 16:12:14,099 debug defaulthandlerexceptionresolver:134 - resolving exception handler [public ba.projectservice.controller.test ba.projectservice.controller.g2bcontroller.savereservation(ba.projectservice.controller.test)]: org.springframework.http.converter.httpmessagenotreadableexception: not read json: unrecognized field "cardnumber" (class ba.projectservice.controller.creditcard), not marked ignorable (5 known properties: , "cvc", "expmonth", "holdername", "cardtype", "expyear"])  @ [source: org.apache.catalina.connector.coyoteinputstream@793ea01d; line: 1, column: 287] (through reference chain: ba.projectservice.controller.test["creditcard"]->ba.projectservice.controller.creditcard["cardnumber"]); nested exception com.fasterxml.jackson.databind.exc.unrecognizedpropertyexception: unrecognized field "cardnumber" (class ba.projectservice.controller.creditcard), not marked ignorable (5 known properties: , "cvc", "expmonth", "holdername", "cardtype", "expyear"])  @ [source: org.apache.catalina.connector.coyoteinputstream@793ea01d; line: 1, column: 287] (through reference chain: ba.projectservice.controller.test["creditcard"]->ba.projectservice.controller.creditcard["cardnumber"]) 

you need serialize javascript object json.stringify.

try:

$.ajax({   type: "post",   url: "http://localhost:8080/services/savereservation",   cache: false,   data: json.stringify(savereservation),   contenttype: "application/json; charset=utf-8",   datatype: "json",   processdata: false,    success: function(data) {     console.debug("after success");   } }); 

and add content type request mappging:

@requestmapping(value = "/savereservation", method = requestmethod.post, consumes=mediatype.application_json_value) 

make sure have defined json message converters. if have jackson dependecy spring you.


your new message clear. sending property javascript not exist in class. not sure whether made mistake or want. property "cardnumber" not exist @ creditcard bean.

you can configure jackson ignore unknown properties without throwing exception using annotation @jsonignoreproperties @ top of class.

@jsonignoreproperties(ignoreunknown = true) private class creditcard {     private string holdername;     private string cardtype;     private string expmonth;     private string expyear;     private string cvc;     //getters setters } 

if want send more 1 credit card use list. so, changing test bean able work list of cards

private class test {     ...     private list<creditcard> creditcards;     ...     //getters setters } 

and @ javascript object, should array

{      ... here properties     "creditcards":[{         "holdername":"card 1",       "cardtype":"discover",       "cardnumber":"6011303031648258",       "expmonth":"01",       "expyear":"2017",       "cvc":"123"    },{         "holdername":"card 2",       "cardtype":"discover",       "cardnumber":"6011303031648258",       "expmonth":"01",       "expyear":"2017",       "cvc":"123"    },{         "holdername":"card 3",       "cardtype":"discover",       "cardnumber":"6011303031648258",       "expmonth":"01",       "expyear":"2017",       "cvc":"123"    }],     ... more properties } 

should enough. hope idea.


Comments

Popular posts from this blog

javascript - Clear button on addentry page doesn't work -

c# - Selenium Authentication Popup preventing driver close or quit -

tensorflow when input_data MNIST_data , zlib.error: Error -3 while decompressing: invalid block type -