spring 笔记1: mvn 中Controller方法的参数不能是嵌套类(内部类)。

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了spring 笔记1: mvn 中Controller方法的参数不能是嵌套类(内部类)。相关的知识,希望对你有一定的参考价值。

最近做spring开发,个人认为,Controller和客户端js通讯时传递的参数类 只使用某几个方法,为了减少对其他功能的影响,想把参数类定义为Controller类的

嵌套类(内部类)。但是实践发现不行。

系统会报错:

Servlet.service() for servlet [kingkoo] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.itl.wms.mvc.systemsetting.POQCSettingController$QueryParameter]: No default constructor found; nested exception is java.lang.NoSuchMethodException: ***************Controller$嵌套类.<init>()] with root cause
java.lang.NoSuchMethodException: com.itl.wms.mvc.systemsetting.Controller$嵌套类.<init>()

 

提示嵌套类没有空参数的构造函数。虽然我已定义类的空构造函数。

因为spring查找参数类型的构造函数时,指明要找一个空参数的构造函数:

return instantiateClass(clazz.getDeclaredConstructor());  //org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:104)
但是,java里面,嵌套类是没有空参数的构造函数的:
public class App {

public class Nest
{
public Nest(){}

}

public static void main(String[] args) throws Exception {

NestClassConstrunct();
}

private static void NestClassConstrunct()
{
java.lang.Class c= Nest.class;
Constructor<?>[] cc= c.getConstructors();
Parameter[] p;
for (int i = 0; i < cc.length; i++) {
System.out.println(cc[i].toString());
p=cc[i].getParameters();
for (int j = 0; j < p.length; j++) {
System.out.println(p[j].getName()+":"+p[j].getParameterizedType().toString());
}
}
}

输出:

public App$Nest(App)
arg0:class App

 

 

so,只能把参数类移动到一个指定的包里面了。

请高手指正。

 

以上是关于spring 笔记1: mvn 中Controller方法的参数不能是嵌套类(内部类)。的主要内容,如果未能解决你的问题,请参考以下文章

mvn spring-boot:run运行不了的解决办法

[spring boot] 01 环境搭建 - 配置java和mvn环境

mvn使用笔记

mvn spring-boot:run 不会在资源中执行 schema.sql

mvn+spring+webapp模板

Spring boot mvn