1

Hi I am trying to read a file as an argument from the main class and accessing the argument in another class in Spring boot. The main class looks like this

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]);

}
}

And I am passing an argument to another class named UsersResources constructor

@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);
}
}

I'm trying to run this code but it's throwing an error saying

java.lang.NoSuchMethodException: Could not find a suitable constructor in com.springsampleapplication.UsersResources class at org.glassfish.jersey.internal.inject.JerseyClassAnalyzer.getConstructor(JerseyClassAnalyzer.java:192) ~[jersey-common-2.25.1.jar:na]

Sudhanva c
  • 109
  • 3
  • 15

3 Answers3

0

The issue might be with Spring trying to initialize UsersResources class and expecting a constructor with no arguments, when you have it only with a String parameter. Try adding such constructor: public UsersResources() {}

Other thought that came into mind is that this could be because of UsersResources constructor not having a visibility modifier (it means it is package protected), you could try adding public visibility modifier to it (though Spring should be able to initialize even private constructors). Are the DemorestApplication and UsersResources classes in the same package though? As otherwise the code should not compile, since UsersResources(String value) is not visible outside of the package it is in. But the error is a bit different in such case: The constructor xxx is undefined, so probably this is not the case.

Adomas
  • 466
  • 1
  • 8
  • 21
  • the DemorestApplication and UsersResources classes are in the same package! @Adomas – Sudhanva c Mar 06 '18 at 16:39
  • I tried adding the public visibility modifier to the UserResources constructor. It's throwing the same error! Could not find a suitable constructor in com.springsampleapplication.UsersResources class , at org.glassfish.jersey.internal.inject.JerseyClassAnalyzer.getConstructor(JerseyClassAnalyzer.java:192) ~[jersey-common-2.25.1.jar:na] @Adomas – Sudhanva c Mar 07 '18 at 05:16
0

Since it seems you are using Jersey you need to have a public constructor:

Root resource classes are instantiated by the JAX-RS runtime and MUST have a public constructor for which the JAX-RS runtime can provide all parameter values. Note that a zero argument constructor is permissible under this rule.

See How to register a static class in Jersey?

Karol Dowbecki
  • 43,645
  • 9
  • 78
  • 111
  • I tried adding the public visibility modifier to the UserResources constructor. It's throwing the same error! Could not find a suitable constructor in com.springsampleapplication.UsersResources class , at org.glassfish.jersey.internal.inject.JerseyClassAnalyzer.getConstructor(JerseyClassAnalyzer.java:192) ~[jersey-common-2.25.1.jar:na]@kdowbecki – Sudhanva c Mar 07 '18 at 05:15
  • Trying your code sample results in `ApplicationContextException: Unable to start EmbeddedWebApplicationContext due to missing EmbeddedServletContainerFactory bean` exception due to my test project setup. Is there anything more to your setup? Can you please provide a [runnable example](https://stackoverflow.com/help/mcve)? – Karol Dowbecki Mar 07 '18 at 07:32
0

Since you have defined a constructor in your class, no default constructor is generated. A work around is to make this class a Spring component with @Component et al.

Gilbert Lopez
  • 163
  • 1
  • 6