示例:User.java
@Component
public class User{
public String name;
public String email;
public User() {
super();
}
public User(String name, String email) {
super();
this.name = name;
this.email = email;
}
public String getName() {
return name;
}
public String getEmail() {
return email;
}
public void setName(String name) {
this.name = name;
}
public void setEmail(String email) {
this.email = email;
}
}
HomeController.java
@Controller
public class HomeController {
@Resource
User user;
@RequestMapping(value="/getUsers", method= RequestMethod.POST)
public @ResponseBody User getUser(User user){
return user;
}
}
但是user.getName()和user.getEmail()为null。用户类未初始化。为什么?即使我在客户端发送用户对象
POST /spring/getUsers HTTP/1.1 Host: localhost:2015 Cache-Control: no-cache {"name":"vinod", "email":"vinod@gmaol.com" }
答案 0 :(得分:1)
以下是示例代码。请仔细阅读文档,了解requestBody注释的工作原理等。
@Controller
public class HomeController {
@RequestMapping(value="/getUsers", method= RequestMethod.POST)
public @ResponseBody User getUser(@RequestBody User user){
return user;
}
}