我试图将对象从客户端发送到服务器。下面是使用spring mvc的控制器。
@Controller
public class HomeController {
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;
}
}
@RequestMapping(value="/getUsers", method= RequestMethod.POST)
public @ResponseBody User getUser(User user){
return user;
}}
在客户端,我在这样的帖子中发送对象 http://localhost:2015/spring/getUsers
POST /spring/getUsers HTTP/1.1
Host: localhost:2015
Cache-Control: no-cache
{"name":"vinod", "email":"vinod@gmaol.com" }
但我在服务器端遇到这样的错误
HTTP Status 500 - Request processing failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [com.vinod.spring.HomeController$User]: No default constructor found; nested exception is java.lang.NoSuchMethodException: com.vinod.spring.HomeController$User.<init>()
答案 0 :(得分:0)
尝试将User类重构为另一个文件,并使用@Resource注释将其引用到控制器中。也许抛出了异常,因为User类是嵌套的。
实施例: 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;
}
}