javascript - Correct way of making POST request with JSON in SPRING MVC? -
i new spring , want correct way of making post request. have list of json object want post server example
var list = [{name:"abc",age:23},{name:"xyz",age:22},{name:"xcx",age:33}]
i making post request in google closure using xhr server in fashion
model.xhrpost(id,url,"list="+json.stringify(this.list),callback);
this controller looks like
@requestmapping(value={"/getinput"}, method = requestmethod.post) @responsebody public string logclienterror(modelmap model, httpservletrequest request, httpservletresponse response) throws exception{ jsonobject result = new jsonobject(); try{ string errorobj = request.getparameter("list"); jsonarray errors = new jsonarray(errorobj); more code here loops through list... result.put("issuccess", true); return result.tostring(); }catch(jsonexception e){ result.put("issuccess", false); return result.tostring(); } }
so in short making post request passing querystring parameter. correct way or should content posted in body? if post in body changes have make ?
this not how should post data rest endpoint. going way can use instead of post , work well. post should used create new resource , content should carried in message body not query param.
on backend side can catch , parse content or create class (see below) filled data body.
dto:
class person { string name integer age } class personlist { list<person> persons }
endpoint:
public string logclienterror(@requestbody personlist list, httpservletrequest request, httpservletresponse response) throws exception
body:
{ "persons": [{name:"abc",age:23},{name:"xyz",age:22},{name:"xcx",age:33}] }
@responsebody
can used in same manner responses.
Comments
Post a Comment