java - ajax POST + spring controller parameter passing error -
i have strange problem passing int param in post request spring mvc controller.
the thing parameter passed through post, didn't parsed controller.
here code: spring controller:
@controller @requestmapping("user") public class usercontroller { @requestmapping(value = "/register", method = requestmethod.post) @responsebody public jsonresponse adduser(@requestbody user user){ system.out.println(user.getcondoid()); system.out.println(user.getpassword()); system.out.println(user.getusername()); *** stuff *** }
user model:
public class user { private string username; private string password; private int condo_id; private list<string> roles; public list<string> getroles() { return roles; } public void setroles(list<string> roles) { this.roles = roles; } public string getusername() { return username; } public void setusername(string username) { this.username = username; } public string getpassword() { return password; } public void setpassword(string password) { this.password = password; } public int getcondoid() { return condo_id; } public void setcondoid(int condo_id) { this.condo_id = condo_id; } }
how send post in angular(example):
$scope.choosencondo.id = "7"; var formdata = { "username" : $scope.registeruser.email, "password" : $scope.registeruser.password, "condo_id" : parseint($scope.choosencondo.id) }; var req = { method: 'post', url: '/api/user/register', headers: { 'x-csrf-token': $('input[name="_csrf"]').val(), 'upgrade-insecure-requests': "1", //'content-type': "application/x-www-form-urlencoded" 'content-type': 'application/json' }, data: formdata } $http(req)
so, system.out.println(user.getcondoid());
receive "0" in log. post data looks example: {"username":"4t654y54y54y","password":"1q2w3e","condo_id":2}
param sent in post. can reason of trouble?
your user model should
public class user { private string username; private string password; private int condo_id; private list<string> roles; public list<string> getroles() { return roles; } public void setroles(list<string> roles) { this.roles = roles; } public string getusername() { return username; } public void setusername(string username) { this.username = username; } public string getpassword() { return password; } public void setpassword(string password) { this.password = password; } public int getcondo_id() { return condo_id; } public void setcondo_id(int condo_id) { this.condo_id = condo_id; } /* public int getcondoid() { return condo_id; } public void setcondoid(int condo_id) { this.condo_id = condo_id; } */ }
it didn't find actual setter method set value property "condo_id".
so, system.out.println(user.getcondoid()); receive "0" in log.
by java nature, primitive value default initialized 0, printing 0
Comments
Post a Comment