未找到默认构造函数;嵌套异常是 java.lang.NoSuchMethodException 与 Spring MVC? [英] No default constructor found; nested exception is java.lang.NoSuchMethodException with Spring MVC?

查看:34
本文介绍了未找到默认构造函数;嵌套异常是 java.lang.NoSuchMethodException 与 Spring MVC?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Spring MVC 控制器项目.下面是我的控制器,我声明了一个构造函数,专门用于测试目的.

I am working with Spring MVC controller project. Below is my Controller and I have a constructor declared which I am specifically using for testing purpose.

@Controller
public class TestController {

    private static KeeperClient testClient = null;

    static {

    // some code here

    }

    /**
     * Added specifically for unit testing purpose.
     * 
     * @param testClient
     */
    public TestController(KeeperClient testClient) {
        TestController.testClient = testClient;
    }

    // some method here

}

每当我启动服务器时,我都会遇到异常 -

Whenever I am starting the server, I am getting below exception -

No default constructor found; nested exception is java.lang.NoSuchMethodException:

但是如果我删除 TestController 构造函数,那么它可以正常工作,没有任何问题.我在这里做错了什么?

But if I remove TestController constructor then it works fine without any problem. What wrong I am doing here?

但是如果我添加这个默认构造函数,它就会开始正常工作 -

But if I add this default constructor then it starts working fine -

    public TestController() {

    }

推荐答案

如果您要创建自己的构造函数,则必须定义无参数或默认构造函数.

You must have to define no-args or default constructor if you are creating your own constructor.

您可以阅读为什么需要默认构造函数或不需要参数构造函数.

You can read why default or no argument constructor is required.

why-default-or-no-argument-constructor-java-class.html

这篇关于未找到默认构造函数;嵌套异常是 java.lang.NoSuchMethodException 与 Spring MVC?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆