Spring Controller and Jquery Ajax are unable to convert Json string to Object -


i have been struggling jquery ajax post data spring controller @requestbody.

i able send json string , capture json string in controller. but, unable send json string jquery ajax controller [which unable convert object].

below code snippet:

 **ajax call:**                function postload(){                 $.ajax({                     type : 'post',                     url : "ajaxhai.do",                     contenttype : "application/json",                     cache : false,                     data : responseobject, **//json-string**                     datatype :"html",                     success : function(data, textstatus) {                         console.log("post success");                         $("#ajaxcontent").html(data);                     },                     error : function(request, status, error) {                         console.log("failed" + error);                     }                 });             } 

// responseobject initialised:

            <c:if test='${not empty ajitems}'>                 var responseobject = json.stringify('${ajitems}');             </c:if>      // ${ajitems}--> model attribute , setting in javascript //send controller through ajax call      //controller:     @requestmapping(value = "ajaxhai.do", method = requestmethod.post,consumes="application/json") **//unable convert requestbody**         public  string testajaxpost(@requestbody ajaxdto[] ajaxres, model model,                 httpservletrequest request, httpservletresponse response) {              system.out.println(ajaxres);              return "ajaxform";         } 

i have jackson dependency in pom:

<dependency>             <groupid>org.codehaus.jackson</groupid>             <artifactid>jackson-mapper-asl</artifactid>             <version>1.7.3</version>         </dependency> 

it saying :

jquery-3.2.1.min.js:4 post http://localhost:8080/testspringmvc-1.0/ajaxhai.do 400 (bad request) 

but when change controller in following way, working charm:

@requestmapping(value = "ajaxhai.do", method = requestmethod.post,consumes="application/json")**//please note : here string , not object[]**     public  string testajaxpost(@requestbody string ajaxres, model model,             httpservletrequest request, httpservletresponse response) {          system.out.println(ajaxres);         gson gson = new gson();         testajaxdto[] mcarray = gson.fromjson(ajaxres, testajaxdto[].class);         list<testajaxdto> mclist = new arraylist<testajaxdto>(arrays.aslist(mcarray));         system.out.println(mclist);         return "ajaxform";     } 

what i'm missing here? have tried possibilities auto json conversion still no luck.

any appreciated.

thank you.

please check json trying send when expecting array in controller. there wrong json. check if have registered mappingjackson2httpmessageconverter assume should have done that.

<bean class="org.springframework.http.converter.json.mappingjackson2httpmessageconverter">         <property name="objectmapper">             <bean class="org.springframework.http.converter.json.jackson2objectmapperfactorybean"                   p:autodetectfields="false"                   p:autodetectgetterssetters="false"                   p:failonemptybeans="false"                   p:indentoutput="true">             </bean>         </property>     </bean> 

here example works fine. ajax request

var requestobject = [{"product_name":"one product","descripction":"essentials","category":null,"price":"100.00","mfg_date":null,"image":null},     {"product_name":"two product","descripction":"essentials 2","category":null,"price":"120.00","mfg_date":null,"image":null}];     $.ajax({             type : 'post',             url : "services/product",             cache : false,             contenttype:"application/json;charset=utf-8",             data : json.stringify(requestobject),//json-string**             datatype :"application/json;charset=utf-8",             success : function(data, textstatus) {                 console.log("post success"+data);                /* $("#ajaxcontent").html(data);*/             },             error : function(request, status, error) {                 console.log("failed" + error);             }         }); 

here controller accept request.

@requestmapping(value = "/product",produces = {mediatype.application_json_utf8_value},             method = requestmethod.post,             consumes = {mediatype.application_json_utf8_value})     public @responsebody     responseentity<list<product>> createproduct(@requestbody  product[] products) {         return new responseentity<list<product>>(arrays.aslist(products), httpstatus.ok);     } 

Comments

Popular posts from this blog

php - Permission denied. Laravel linux server -

google bigquery - Delta between query execution time and Java query call to finish -

python - Pandas two dataframes multiplication? -