我无法摆脱错误:
HTTP Status 500 - Request processing failed; nested exception is
org.springframework.beans.BeanInstantiationException:
Could not instantiate bean class [[Lmain.java.com.springapp.mvc.model.DSLR;]:
No default constructor found;
nested exception is java.lang.NoSuchMethodException: [Lmain.java.com.springapp.mvc.model.DSLR;.<init>()
Run Code Online (Sandbox Code Playgroud)
我实际上有DSLR类的构造函数.我的代码出了什么问题?
代码在这里> Spring MVC WebApp:http://goo.gl/ddhLg5
可能导致错误的DSLR类:
package main.java.com.springapp.mvc.model;
import org.springframework.beans.factory.annotation.Autowired;
public class DSLR {
public DSLR() {
}
private int dslrId;
private String model;
private int price;
private String description;
public int getDslrId() {
return dslrId;
}
public void setDslrId(int dslrId) {
this.dslrId = dslrId;
}
public String getModel() {
return model;
}
public void setModel(String model) {
this.model = model;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
@Override
public String toString() {
return "DSLR [dslr=" + dslrId + ", model=" + model
+ ", price=" + price+ ", description=" + description+"]";
}
}
Run Code Online (Sandbox Code Playgroud)
在实例化DSLR的DSLRServletController中,我进行了更改:
@ModelAttribute("dslrs")DSLR dslrs []
变成:
@ModelAttribute("dslrs")列出dslrs
摆脱以前的错误并给出:
org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [java.util.List]: Specified class is an interface
解决方案:Spring MVC Web应用程序:没有找到默认构造函数 如果有人可以在这里总结并写下答案,我很乐意接受!
为了使某些框架以这种方式初始化对象,您必须提供默认构造函数(不带参数的构造函数),即使它没有做任何事情.
这是因为你可能在那里有另一个至少需要一个参数的构造函数.从逻辑上讲,库不知道传递给传递给它的每个任意类的参数.
这在错误中表示,它表示java.lang.NoSuchMethodException: [Lmain.java.com.springapp.mvc.model.DSLR;.<init>():
NoSuchMethodException 意味着它无法在运行时找到它所期望的方法(通过反射).<init>()指构造函数(构造函数在技术上不具有名称,因为它们始终只是类本身的名称;因此,JVM将它们称为<init>()).由于DSLR您与我们共享的课程不可能出现错误,我的猜测是您的服务器可能包含旧课程.
我建议在IDE中清理项目以及Tomcat安装.