0

I have strange problem with passing an Int param in POST request to the Spring MVC controller.

The thing is that a parameter is passed through POST, but didn't parsed by the controller.

Here is the 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());

        *** SOME MY 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 do i 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, for System.out.println(user.getCondoID()); i receive "0" in log. But the POST data looks like this for example: {"username":"4t654y54y54y","password":"1q2w3e","condo_id":2} so the param is sent in POST. What can be a reason of this trouble?

6
  • 3
    Maybe your getter/setter for condo_id variable are not following the nomenclature conventions, so condo_id cannot be serialized correclty. Could you try it changing these methods for getCondo_id() and setCondo_id(int condo_id)? or changing the variable name from condo_id to condoId. Commented Aug 25, 2015 at 8:25
  • another guess - User class may need default constructor. Commented Aug 25, 2015 at 8:49
  • "Could you try it changing these methods" change it to what? Commented Aug 25, 2015 at 8:51
  • getCondoID() -> getCondo_id() and setCondoID(int condo_id) -> setCondo_id(int condo_id). Commented Aug 25, 2015 at 9:00
  • Use proper camelcase for your variables/methods. int condo_id would become Integer condoId (prefer to use Objects rather then primitives). Next let your IDE generate the getters/setters. Commented Aug 25, 2015 at 9:02

1 Answer 1

1

Your User Model should be like

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 the actual setter method to set the value for the property "condo_id".

So, for System.out.println(user.getCondoID()); i receive "0" in log. 

By java nature, primitive value default initialized to 0, so it is printing 0

Sign up to request clarification or add additional context in comments.

4 Comments

private Integer condo_id; Declare as Interger thats what helps :)
When you define as private Integer condo_id;, If there is no values coming it will be initialized to null,
When you define as private int condo_id;, If there is no values coming it will throw exception, NULL can not be assigned to primitive type
value is coming, it wasn't parsed. private Integer condo_id; this helped

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.