Java 为什么我在 AngularJs 帖子中收到 400 个错误的请求?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26307730/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 02:10:51  来源:igfitidea点击:

Why am I getting 400 bad request with AngularJs post?

javaangularjsspringspring-mvc

提问by Pracede

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.

我使用 Spring MVC 和 AngularJs 来创建一个 Web 应用程序。我向服务器发送请求,但我收到400 error bad request. 我在Spring servlet配置文件中为json格式配置了消息转换器。我想知道为什么我会收到这个错误。

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 :

在服务器端,我有一个 Spring MVC 控制器,如下所述:

@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 @RequestBodyin the controller I don't have a 400 error but the user is null:

我注意到其他一些事情:当我删除@RequestBody控制器中的 时,我没有 400 错误,但用户是null

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

采纳答案by Pracede

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.

问题出在用户表单上。我有姓氏、名字、电子邮件、密码、密码 1,但用户 Java 对象不包含密码 1 属性。当请求提供的json数据与Java Object不对应时,JsonConverter无法将数据匹配到Java Object。