【问题标题】:Spring Boot - Could not find a suitable constructor errorSpring Boot - 找不到合适的构造函数错误
【发布时间】:2018-08-14 12:08:21
【问题描述】:

您好,我正在尝试从主类中读取文件作为参数,并在 Spring Boot 中访问另一个类中的参数。 主类是这样的

public class DemorestApplication extends SpringBootServletInitializer {

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

    new DemorestApplication().configure(new SpringApplicationBuilder(DemorestApplication.class)).run(args);

    new UsersResources(args[0]);

}
}

我正在将一个参数传递给另一个名为 UsersResources 构造函数的类

@Path("/sample")
public class UsersResources {

private String value;
UsersResources(String value){
    this.value=value;
}

//new code
@GET
@Path("Data/file/{path}")
@Produces("application/json")

public Map<String, Map<String, List<Map<String, Map<String, String>>>>> getApplicationName1(@PathParam("path") String path) throws IOException  {
ReadExceldy prop = new ReadExceldy();
FileInputStream Fs = new FileInputStream(System.getProperty("user.dir")+"\\"+value);
Properties properties = new Properties();
properties.load(Fs);
String Loc=properties.getProperty("filepath"); 
String Na=path;
String filename=Loc+Na;
return prop.fileToJson(filename);
}
}

我正在尝试运行此代码,但它抛出一个错误提示

java.lang.NoSuchMethodException:在 com.springsampleapplication.UsersResources 类中找不到合适的构造函数 在 org.glassfish.jersey.internal.inject.JerseyClassAnalyzer.getConstructor(JerseyClassAnalyzer.java:192) ~[jersey-common-2.25.1.jar:na]

【问题讨论】:

标签: java spring-boot constructor jersey command-line-arguments


【解决方案1】:

问题可能在于 Spring 尝试初始化 UsersResources 类并期望一个没有参数的构造函数,而您只有一个 String 参数。尝试添加这样的构造函数:public UsersResources() {}

想到的其他想法是,这可能是因为 UsersResources 构造函数没有可见性修饰符(这意味着它是包保护的),您可以尝试向其添加 public 可见性修饰符(尽管 Spring 应该是甚至可以初始化私有构造函数)。 DemorestApplicationUsersResources 类是否在同一个包中?否则代码不应该编译,因为UsersResources(String value) 在它所在的包之外是不可见的。但在这种情况下错误有点不同:The constructor xxx is undefined,所以可能不是这种情况。

【讨论】:

  • DemorestApplication 和 UsersResources 类在同一个包中! @Adomas
  • 我尝试将 public 可见性修饰符添加到 UserResources 构造函数。它抛出了同样的错误!在 org.glassfish.jersey.internal.inject.JerseyClassAnalyzer.getConstructor(JerseyClassAnalyzer.java:192) 的 com.springsampleapplication.UsersResources 类中找不到合适的构造函数 ~[jersey-common-2.25.1.jar:na] @阿多玛斯
【解决方案2】:

由于您似乎在使用 Jersey,您需要有一个 public 构造函数:

根资源类由 JAX-RS 运行时实例化,并且必须有一个公共构造函数,JAX-RS 运行时可以为其提供所有参数值。请注意,此规则允许零参数构造函数。

How to register a static class in Jersey?

【讨论】:

  • 我尝试将 public 可见性修饰符添加到 UserResources 构造函数。它抛出了同样的错误!在 org.glassfish.jersey.internal.inject.JerseyClassAnalyzer.getConstructor(JerseyClassAnalyzer.java:192) 的 com.springsampleapplication.UsersResources 类中找不到合适的构造函数~[jersey-common-2.25.1.jar:na]@ kdowbecki
  • 由于我的测试项目设置,尝试您的代码示例会导致 ApplicationContextException: Unable to start EmbeddedWebApplicationContext due to missing EmbeddedServletContainerFactory bean 异常。您的设置还有其他内容吗?你能提供一个runnable example吗?
【解决方案3】:

由于您在类中定义了构造函数,因此不会生成默认构造函数。一种解决方法是使用 @Component 等使此类成为 Spring 组件。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-09-23
    • 2023-03-13
    • 2022-01-11
    • 2016-03-24
    • 2018-01-12
    • 1970-01-01
    相关资源
    最近更新 更多