Why am I getting 400 bad request with AngularJs post?

22,505

The problem was about the user form. I had lastname, firstname, email, password, password1 but the User Java Object does not contains password1 attributes. When json data provide by the request does not correspond to the Java Object, the JsonConverter is not able to match to data to Java Object.

Share:
22,505
Pracede
Author by

Pracede

Java,J2EE, Spring, Spring, Hibernate,AngularJS, Javascript, Jquery, Bootstrap,NodeJS, MongoDB, Express

Updated on April 09, 2020

Comments

  • Pracede
    Pracede about 4 years

    I use Spring MVC and AngularJs to create a web application. I send a request to the server but I am getting 400 error bad request. I configure the message converter for the json format in the Spring servlet configuration file. I am wondering why I am getting this error.

    Here is my angular service :

    save : function(user) {
        return $http({
            method: 'POST',
            url: '/app-web/user/create',
            contentType: "application/json",
            data:user
        });
    }
    

    And on the server side I have a Spring MVC Controller as described below :

    @RequestMapping(value="/user/create", method= RequestMethod.POST)
    @ResponseBody
    public String createAccount(@RequestBody User user){
      //some logic
        return "Ok";
    }
    

    I noticed something else: when I remove the @RequestBody in the controller I don't have a 400 error but the user is null:

    @RequestMapping(value="/user/create", method= RequestMethod.POST)
    @ResponseBody
    public String createAccount(User user){
      //some logic
        return "Ok";
    }