java - How To Receive JSON Data In Cross Origin Resource Sharing (CORS) with Spring REST -


i trying send / receive json data from / to controller class in project.

the controller class of project follows :

@requestmapping(value = "/dummy/",method = requestmethod.post,headers="accept=application/json") public response getdummyjson() {      /*  method 2: getting dummy json data file , sending json object  */      jsonparser parser = new jsonparser();     jsonobject jsonobject = null;     try {          object obj = parser.parse(new filereader("d:\\jsonstruc.json"));         jsonobject = (jsonobject) obj;      } catch (exception e) {         system.out.println("error parsing: - ");     }              return response.status(200).entity(jsonobject).build();               }  @requestmapping(value = "/insert/dummy/",method= requestmethod.post,headers="accept=application/json") public response setdummyjson(@requestbody jsonobject inputjsonobj){      /*step 1: display json data sent angularjs */      system.out.println("data received:"+ inputjsonobj.tostring());     jsonparser jsonparser = new jsonparser();     containerfactory containerfactory = new containerfactory(){         public list creatarraycontainer() {             return new linkedlist();         }          public map createobjectcontainer() {             return new linkedhashmap();         }      };      map obj = (map)inputjsonobj;     iterator iter = obj.entryset().iterator();     while(iter.hasnext()){         map.entry entry = (map.entry)iter.next();         system.out.println(entry.getkey() + "=>" + entry.getvalue());      }      /* step 2: send next dummy json file */     jsonparser parser = new jsonparser();     jsonobject jsonobject1 = null;     try {          object obj1 = parser.parse(new filereader("d:\\jsonstruc1.json"));         jsonobject1 = (jsonobject) obj1;      } catch (exception e) {         system.out.println("error parsing: - ");     }        return response.status(200).entity(jsonobject1).build();            }   

the cors class of project follows :

public class corsfilter extends onceperrequestfilter{   private properties prop = new properties();      @override     protected void dofilterinternal(httpservletrequest request, httpservletresponse response, filterchain filterchain)              throws servletexception, ioexception {            system.out.println("inside cross origin resource sharing filter class");          system.out.println(request.getcontenttype());          system.out.println(request.getcontentlength());           response.setheader("access-control-allow-origin", "*");          response.setheader("access-control-allow-methods", "post, get, options, delete, head, put");          response.setheader("access-control-max-age", "3600");          response.setheader("access-control-allow-credentials", "true");          response.setheader("access-control-allow-headers", "x-requested-with");           filterchain.dofilter(request, response);     }  } 

there no problem regarding sending dummy json data through url (/service/question/dummy/) when trying json data (/service/question/insert/dummy/) , send next json object through method setdummyjson(....) having trouble. angularjs (client) side showing following exception message :

cross-origin request blocked: same origin policy disallows reading remote resource @ /fetchquestions/service/question/insert/dummy/. can fixed moving resource same domain or enabling cors.

on invoking url (/service/question/insert/dummy/) localhost json string content body following error message thrown :

the server refused request because request entity in format not supported requested resource requested method ().

what doing wrong ? appreciated. thanks.

there nothing wrong above code.

for reason whenever content-type set application/json in angularjs(client side) using following command :

$httpprovider.defaults.headers.post['content-type'] = 'application/json';

the server side received content-type of null. other content-type application/x-www-form-urlencoded or text/plain works problem occurs application/json (even though angularjs sending json data server side).

thus little change in controller class fixed problem.

@requestmapping(value = "/insert/dummy/",method= requestmethod.post") public response setdummyjson(@requestbody string inputjsonobj){ /* step1: receive data in text/plain format    step2: conver string json object. necessary processing.    step3: return dummy json object through response. */   return response.status(200).entity(jsonobject1).build(); } 

Comments

Popular posts from this blog

matlab - "Contour not rendered for non-finite ZData" -

delphi - Indy UDP Read Contents of Adata -

javascript - Any ideas when Firefox is likely to implement lengthAdjust and textLength? -