Car*_*shf 5 java rest spring json angularjs
我也在使用Spring使用AngularJS和WildFly开发Web应用程序。
我的问题是我要疯了,因为注释@requestBody似乎工作错误。
这是我的服务:
@ResponseBody
@RequestMapping(value = "/keyuser", method = RequestMethod.POST,
consumes = "application/json")
public KeyProfileUserSummary updateEmployee(@RequestBody KeyProfileUserSummary keyUser) {
return null;
}
Run Code Online (Sandbox Code Playgroud)
这是我的对象KeyProfileUserSummary的成员:
private Integer id;
private String login;
private String password;
private String firstname;
private String lastname;
private UserRole userRole;
Run Code Online (Sandbox Code Playgroud)
我不知道发生了什么,但是我已经用其他类型的对象测试了该服务,并且效果很好,但是在定义KeyProfileUserSummary时,它不起作用,我收到错误400错误请求。我已经测试过将@RequestBody设置为“ Object”,以便至少可以看到即将发生的事情,并且从前端开始,我得到以下信息:
{id=3, login=aa, password=a, firstname=Martin, lastname=Müller, userRole=ROLE_USER}
Run Code Online (Sandbox Code Playgroud)
UserRole是一个枚举。需要澄清的一点很重要,就是KeyProfileUserSummary只是KeyProfileUser的摘要版本,但是由于我在响应中得到了所有链接的元素,因此我决定发送此较轻的类。使用KeyProfileUser进行的测试非常完美,我在Angular端获得了JSON对象并将其发送回去。
在Angular方面,我对该对象不做任何事情。只需将其接收到列表中,然后按编辑按钮即可将列表中的元素发送回去。这是我发送的方式:
res = $http.post("url.../keyuser", user);
Run Code Online (Sandbox Code Playgroud)
问题是我可以使所有内容与KeyProfileUser完美配合,但是由于数据库确实非常庞大且引用很多,所以我决定切换到这个较轻的类,但是现在我只收到此ERROR 400 BAD REQUEST ...而且我要吊死自己:P
谢谢你的帮助!
好的,最后我找到了解决方案。
在我的 KeyProfileUserSummary 中,我只有一个构造函数,它采用 KeyProfileUser 并将属性设置为摘要版本:
public KeyProfileUserSummary(KeyProfileUser keyProfileUser) {
this.id = keyProfileUser.getId();
this.login = keyProfileUser.getLogin();
this.password = keyProfileUser.getPassword();
this.firstname = keyProfileUser.getPerson().getFirstname();
this.lastname = keyProfileUser.getPerson().getLastname();
this.userRole = keyProfileUser.getUserRole();
}
Run Code Online (Sandbox Code Playgroud)
显然,在调度程序 servlet 的第 993 行设置断点(感谢@Clemens Eberwein 的提示),我意识到从 JSON 对象解析时,Jackson 解析器需要一个空的构造函数!所以添加它解决了它并且完美地工作。
注意:对于 KeyProfileUser,它工作得很好,因为我们有用于 hibernate 的注释 @Entity,因此自动创建了空构造函数。
归档时间: |
|
查看次数: |
5071 次 |
最近记录: |