4

I have a class of which only static methods are to be accessed via @path annotations and which does not have a public constructor. My simpilified program is:

@Path("")
static class MyStaticClass
{
  private MyStaticClass() {...}
 @Get @Path("time")
  static public String time()
  {
     return Instant.now().toString();
  }
}

Running and calling "time" gives me the following error:

WARNUNG: The following warnings have been detected: WARNING: HK2 service reification failed for [...] with an exception:
MultiException stack 1 of 2
java.lang.NoSuchMethodException: Could not find a suitable constructor in [...] class.
Konrad Höffner
  • 11,100
  • 16
  • 60
  • 118

2 Answers2

3

Sorry, according to the JSR, paragraph 3.1.2

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.

You can use the Adapter design pattern and create JAX-RS resource (POJO with @Path) which simply delegates to your static class. This would be very easy to understand for those coming behind you.

Steve Siebert
  • 1,874
  • 12
  • 18
  • I'm having trouble understanding this answer, though I'm convinced it is the cause of my issue. An example would be great. I'm using a static inner class which is probably the issue. It used to work with my older Jersey libraries. I only started having this issue with Jersey 2.26 after being forced to upgrade. – Sridhar Sarnobat Dec 22 '17 at 07:07
  • Actually, my issue was I renamed my Main class but was still calling the old Main class. So mine was a result of not using maven correctly. The hidden dangers of renaming a class... – Sridhar Sarnobat Dec 22 '17 at 07:14
1

The @Path annotation is designed to define a resource at the class level. The method to execute isn't controlled by @Path, but by @GET, @POST, @PUT, @HEAD, etc... with @GET as the desired operation in your case.

Your class for the "time" resource should look like this:

@Path("/time")
public class TimeResource { 
    @GET
    public static String time(){
        return Instant.now().toString();
    }
}

You could theoretically define each function as a static nested class within one "main" class:

public class MyResource{

    @Path("/time")
    public static final class TimeResource {    
        @GET
        public static String do(){
            return Instant.now().toString();
        }
    }

    @Path("/doSomethingElse")
    public static final class DoSomethingElseResource { 
        @GET
        public static String do(){
            // DO SOMETHING ELSE
        }
    }       
}

Though I don't know if that would work, you'd have to try it. I don't think there's much advantage in having them all in one class like that, though.

Steve K
  • 4,863
  • 2
  • 32
  • 41