错误:java.lang.NoSuchMethodException: java.lang.Long.<init>() in spring MVC
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35807064/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Error: java.lang.NoSuchMethodException: java.lang.Long.<init>() in spring MVC
提问by Khan
Getting this error while reading student object from database.
从数据库读取学生对象时出现此错误。
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)
full stack trace:
完整的堆栈跟踪:
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
学生.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
控制器.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
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;
}
}
采纳答案by achabahe
The @ModelAttribute("studentId") Long studentId
is the source of the problem, because spring doesn't find a method that can provide this Long
object, so it tries to instantiate one and pass it as a method argument. To solve this problemyou can either :
的@ModelAttribute("studentId") Long studentId
是问题的根源,因为弹簧不会发现可以提供这样的一种方法Long
的对象,所以它试图实例之一,并把它作为一个方法参数。要解决此问题,您可以:
Delete @ModelAttribue from the method argument
@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"; }
Create a method that will provide that
Long
Objectin your controlle@ModelAttribute public void provideStudentId(Model model){ model.addAttribute("studentId", new Long(1)); }
从方法参数中删除@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)); }
Official Doc
官方文件
@RequestMapping(path = "/owners/{ownerId}/pets/{petId}/edit", method = RequestMethod.POST)
public String processSubmit(@ModelAttribute Pet pet) { }
Given the above example where can the Pet instance come from? There are several options:
- It may already be in the model due to use of @SessionAttributes?—?see the section called “Using @SessionAttributes to store model attributes in the HTTP session between requests”.
- It may already be in the model due to an @ModelAttribute method in the same controller?—?as explained in the previous section.
- It may be retrieved based on a URI template variable and type converter (explained in more detail below).
- It may be instantiated using its default constructor.
鉴于上面的例子,Pet 实例来自哪里?有几种选择:
- 由于使用了@SessionAttributes,它可能已经在模型中?—?请参阅“使用@SessionAttributes 在请求之间的 HTTP 会话中存储模型属性”一节。
- 由于同一控制器中的@ModelAttribute 方法,它可能已经在模型中?-?如上一节所述。
- 可以基于 URI 模板变量和类型转换器(下面更详细地解释)来检索它。
- 它可以使用其默认构造函数进行实例化。
EDIT
If the studentId was the parameter name sent from the UI you can use @RequestParam
like this
编辑
如果 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";
}
回答by Nir Levy
You should change your studentId
field type from Long
to long
, it should fix it.
您应该将studentId
字段类型从更改Long
为long
,它应该可以修复它。
And one more unrelated thing that just pops to my eyes, this:
还有一件不相关的事情突然出现在我的眼前,这个:
if (student!=null) {
return student;
} else {
return null;
}
Is exactly like saying this:
就像这样说:
return student;