这是我的代码,我试图将用户详细信息作为json输入传递,但我无法通过rest api方法接收数据。我将所有值都设为null
这是我的json请求
{
"userId" : "12345",
"username" : "arun.ammasai",
"createdBy" : "-2",
"updatedBy" : "-2",
"statusCd" : "New",
"createdDate" : "2019-03-03",
"updatedDate" : "2019-03-03"
}
================================================ =====================
@RequestMapping(value = "/registerUser", method = RequestMethod.POST, consumes = { "application/JSON", "application/XML" })
private String registerUser(User user) {
System.out.println(user.toString());
return "User Created";
}
================================================ =====================
这是Postman Client中的回复
意外的“ U”
答案 0 :(得分:1)
使用@RequestBody批注更新您的方法签名。它将自动将json反序列化为Java实体。请注意,json中的名称应与User对象中的参数相同,并且User对象应具有getter和setter。因此您的方法应类似于
@RequestMapping(value = "/registerUser", method = RequestMethod.POST, consumes = { "application/JSON", "application/XML" })
private String registerUser(@RequestBody User user) {
System.out.println(user.toString()); //What is the reason of doing toString of java Object?
//better to do System.out.println(user.getUsername())
return "User Created";
}