未找到默认构造函数;嵌套异常是 java.lang.NoSuchMethodException 与 Spring MVC?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25272543/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 00:02:07  来源:igfitidea点击:

No default constructor found; nested exception is java.lang.NoSuchMethodException with Spring MVC?

javaspringspring-mvcmodel-view-controller

提问by john

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.

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

@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:

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

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

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

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

    public TestController() {

    }

采纳答案by prashant thakre

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-class.html

回答by Ricardo Veguilla

Spring cannot instantiate your TestController because its only constructor requires a parameter. You can add a no-arg constructor or you add @Autowired annotation to the constructor:

Spring 无法实例化您的 TestController,因为它唯一的构造函数需要一个参数。您可以添加无参数构造函数或向构造函数添加 @Autowired 注释:

@Autowired
public TestController(KeeperClient testClient) {
    TestController.testClient = testClient;
}

In this case, you are explicitly telling Spring to search the application context for a KeeperClient bean and inject it when instantiating the TestControlller.

在这种情况下,您明确告诉 Spring 在应用程序上下文中搜索 KeeperClient bean 并在实例化 TestControlller 时注入它。

回答by MK Yung

In my case, spring threw this because i forgot to make an inner class static.

就我而言,spring 抛出了这个,因为我忘记将内部类设为静态。

When you found that it doesnt help even adding a no-arg constructor, please check your modifier.

当您发现即使添加无参数构造函数也无济于事时,请检查您的修饰符。

回答by IKo

In my case I forgot to add @RequestBodyannotation to the method argument:

在我的情况下,我忘记@RequestBody在方法参数中添加注释:

public TestController(@RequestBody KeeperClient testClient) {
        TestController.testClient = testClient;
    }

回答by Manabu Tokunaga

If your environment is using bothGuice and Spring and using the constructor @Inject, for example, with Play Framework, you will also run into this issue if you have mistakenly auto-completed the import with an incorrect choice of:

如果您的环境是使用两种Guice和Spring和使用构造@注入,例如,与游戏框架,你也会碰到这个问题,如果你有误自动完成了一个不正确的选择进口:

import com.google.inject.Inject;

Then you get the same missing default constructorerror even though the rest of your source with @Inject looks exactly the same way as other working components in your project and compile without an error.

然后,missing default constructor即使使用 @Inject 的其余源代码看起来与项目中的其他工作组件完全相同,并且编译时没有错误,您也会收到相同的错误。

Correct that with:

用以下方法更正:

import javax.inject.Inject;

Do not write a default constructor with construction time injection.

不要编写带有构造时间注入的默认构造函数。