jquery - ajax POST call to controller gives null value in spring -


i sending ajax post request controller. call goes through,however, value sent parameter null.

ajax jquery call:

var invalueurl = "/list/invalue"; var filter_data = {         supplierid: 1     };       $filtersuppliername.on('change',function(){          console.log("before ajax request");             $.ajax({                 url: invalueurl,                  type: 'post',                 data:json.stringify(filter_data),                 contenttype: "application/json",                 success: function (response) {                     $("table").html(response);                 },                 error: function () {                     //your error code                 }             });         }); 

controller:

@requestmapping(value = "invalue", method = requestmethod.post,headers = "accept=application/json")     @responsebody     public string redire(@requestbody value<integer> supplierid ){         system.out.println(supplierid.getvalue()); // null         return "success1";     } 

value class:

public class value<t> {      public value() {}      public value(t t) {         this.value = t;     }      private t value;      public t getvalue() {         return value;     }      public void setvalue(t value) {         this.value = value;     } } 

why supplierid remain null. in advance.

the reason controller method argument, value<integer> not null, value.getvalue() null, spring tries map request parameters domain objects name. passing {supplierid=1} data, target object, value, has no setsupplierid method, object instantiated, no values set.

there couple solutions problem, depending on needs.

1. use @requestparam

it seem easiest solution here change method signature expect integer named supplierid:

@requestmapping(value = "invalue", method = requestmethod.post,headers = "accept=application/json") @responsebody public string redire(@requestparam integer supplierid ){     system.out.println(supplierid); // no longer null     return "success1"; } 

2. use map

if controller not know type of data receive, can toss of input map , handle input parameters appropriately:

@requestmapping(value = "invalue", method = requestmethod.post,headers = "accept=application/json") @responsebody public string redire(@requestbody map<string,string> data ){     if (data.containskey("supplierid")){         system.out.println(integer.parseint(data.get("supplierid"))); // no longer null     }     return "success1"; } 

though not strictly necessary, since httpservletrequest object present in controller methods contains map of input parameters:

@requestmapping(value = "invalue", method = requestmethod.post,headers = "accept=application/json") @responsebody public string redire(httpservletrequest request){     if (request.getparametermap.containskey("supplierid")){         system.out.println(integer.parseint(request.getparametermap.get("supplierid")[0])); // no longer null     }     return "success1"; } 

3. use custom handlermethodargumentresolver

if absolutely have use value class, can extending handlermethodargumentresolver class, responsible mapping http request parameters java objects. there several questions on stack overflow tackle subject.


Comments