i need send data spring mvc controller ajax. controller doesn't work if send more 1 parameter.
controller method:
@timed @requestmapping(value = "saveee", method = requestmethod.post) @responsebody public jsonresultbean savetickettemplate(@requestbody tickettemplatefieldbean fieldbean, long id) throws ioexception { //todo smth return jsonresultbean.success(); }
with ajax code works perfectly:
$.ajax({ type: 'post', datatype: 'json', contenttype: 'application/json', url: '/organizer/api/saveee', data: json.stringify(fieldbean.data), success: function(result) { //todo } })
but if change data parameters, controller doesn't request even.
data: ({'fieldbean': json.stringify(fieldbean.data), 'id': id})
what i'm doing wrong?
that won't work. first lets clarify difference between @requestbody , @requestparam.
the @requestbody method parameter annotation should bind json value in http request body java object using httpmessageconverter. httpmessageconverter responsible converting http request message assoicated java object. source
and use @requestparam annotation bind request parameters method parameter in controller. source
coming question... first ajax request sending json controller not request parameters, @requestbody ok.
in second ajax request sending json 2 fields (fieldbean , id). since @requestbody annotated parameter expected hold entire body of request , bind 1 object. should modify java object(ie tickettemplatefieldbean) hold id field also. work if have 1 argument in controller.
then, how have second argument?
you cannot use 2 @requestbody :
public jsonresultbean savetickettemplate(@requestbody tickettemplatefieldbean fieldbean, @requestbody long id).
as can bind single object (the body can consumed once), cannot pass multiple separate json objects spring controller. instead must wrap in single object.
so solution pass request parameter- @requestparam, or path variable - @pathvariable. since @requestparam , @modelattribute work when data submitted request parameters. should change code this:
@timed @requestmapping(value = "saveee", method = requestmethod.post) @responsebody public jsonresultbean savetickettemplate(@requestbody tickettemplatefieldbean fieldbean, @requestparam("id") long id) throws ioexception { //todo smth return jsonresultbean.success(); }
and change request url follows:
$.ajax({ type: 'post', datatype: 'json', contenttype: 'application/json', url: '/organizer/api/saveee?id=10', data: json.stringify(fieldbean.data), success: function(result) { //todo } })
you can use @pathvariable this:
@timed @requestmapping(value = "saveee/{id}", method = requestmethod.post) @responsebody public jsonresultbean savetickettemplate(@requestbody tickettemplatefieldbean fieldbean, @pathvariable("id") long id) throws ioexception { //todo smth return jsonresultbean.success(); }
and change request url follows:
$.ajax({ type: 'post', datatype: 'json', contenttype: 'application/json', url: '/organizer/api/saveee/10', data: json.stringify(fieldbean.data), success: function(result) { //todo } })
Comments
Post a Comment