从数据库中读取学生对象时出现此错误。
org.springframework.beans.BeanInstantiationException:无法实例化[java.lang.Long]:找不到默认构造函数;嵌套异常是java.lang.NoSuchMethodException:java.lang.Long。() org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:107)
完整堆栈跟踪:
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [java.lang.Long]: No default constructor found; nested exception is java.lang.NoSuchMethodException: java.lang.Long.<init>()
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:978)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:857)
javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:842)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
root cause
org.springframework.beans.BeanInstantiationException: Failed to instantiate [java.lang.Long]: No default constructor found; nested exception is java.lang.NoSuchMethodException: java.lang.Long.<init>()
org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:107)
org.springframework.web.bind.annotation.support.HandlerMethodInvoker.resolveModelAttribute(HandlerMethodInvoker.java:775)
org.springframework.web.bind.annotation.support.HandlerMethodInvoker.resolveHandlerArguments(HandlerMethodInvoker.java:368)
org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:172)
org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:446)
org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:434)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:943)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:877)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:966)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:857)
javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:842)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
root cause
java.lang.NoSuchMethodException: java.lang.Long.<init>()
java.lang.Class.getConstructor0(Class.java:3082)
java.lang.Class.getDeclaredConstructor(Class.java:2178)
org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:104)
org.springframework.web.bind.annotation.support.HandlerMethodInvoker.resolveModelAttribute(HandlerMethodInvoker.java:775)
org.springframework.web.bind.annotation.support.HandlerMethodInvoker.resolveHandlerArguments(HandlerMethodInvoker.java:368)
org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:172)
org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:446)
org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:434)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:943)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:877)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:966)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:857)
javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:842)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
Student.java
@Entity
@Table(name="Student")
public class Student implements Serializable{
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name="studentId")
Long studentId;
@Column(name="studentName")
String studentName;
Controller.java
@RequestMapping(value = "/read.html")
public String readStudent(Model model, @ModelAttribute("studentId") Long studentId) {
Student student = null;
studentId = 2l;
try{
student = serviceFile.readStudent(studentId);
}catch(Exception e){
model.addAttribute("message", "Some thing went wrong !!!! Exception occoured");
return "message";
}
model.addAttribute("student", student);
return "read";
}
daoImpl.java
@Repository
@Transactional
public class DaoImplFile implements DaoFile {
private EntityManager entityManager;
public EntityManager getEntityManager() {
return entityManager;
}
@PersistenceContext
public void setEntityManager(EntityManager entityManager) {
this.entityManager = entityManager;
}
@Override
public Student read(Long studentId) throws NullPointerException {
Student student = entityManager.find(Student.class, studentId);
if (student!=null) {
return student;
} else {
return null;
}
}
答案 0 :(得分:5)
@ModelAttribute("studentId") Long studentId
是问题的根源,因为Spring找不到可以提供此Long
对象的方法,所以它尝试实例化一个并将其作为方法参数传递。 要解决此问题,您可以:
从方法参数中删除@ModelAttribue
@RequestMapping(value = "/read.html")
public String readStudent(Model model,Long studentId) {
Student student = null;
studentId = 2l;
try {
student = serviceFile.readStudent(studentId);
} catch(Exception e){
model.addAttribute("message", "Some thing went wrong !!!! Exception occured");
return "message";
}
model.addAttribute("student", student);
return "read";
}
创建一个提供Long
对象的方法
在你的控制中
@ModelAttribute
public void provideStudentId(Model model){
model.addAttribute("studentId", new Long(1));
}
官方文件
@RequestMapping(path = "/owners/{ownerId}/pets/{petId}/edit", method = RequestMethod.POST)
public String processSubmit(@ModelAttribute Pet pet) { }
鉴于上面的例子,Pet实例可以来自哪里?有几种选择:
- 由于使用了@SessionAttributes,它可能已经在模型中 - 请参阅 “使用@SessionAttributes存储模型属性”一节 在请求之间的HTTP会话“。
- 由于同一个控制器中的@ModelAttribute方法,它可能已经在模型中 - 如上一节所述。
- 可以基于URI模板变量和类型转换器检索它(在下面更详细地解释)。
- 可以使用其默认构造函数进行实例化。
醇>
修改强>
如果studentId是从UI发送的参数名称,则可以使用@RequestParam
这样的
@RequestMapping(value = "/read.html")
public String readStudent(Model model, @RequestParam("studentId") Long studentId) {
Student student = null;
studentId = 2l;
try {
student = serviceFile.readStudent(studentId);
} catch(Exception e) {
model.addAttribute("message", "Some thing went wrong !!!! Exception occoured");
return "message";
}
model.addAttribute("student", student);
return "read";
}
答案 1 :(得分:0)
您应该将studentId
字段类型从Long
更改为long
,它应该修复它。
还有一个与我眼睛无关的事情,这个:
if (student!=null) {
return student;
} else {
return null;
}
就像这样说:
return student;