How to register a static class in Jersey?

I have a class that only static methods should access through annotations @pathand which does not have a common constructor. My simplified program:

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

Starting 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.
+4
source share
2 answers

Sorry, according to JSR , paragraph 3.1.2

Resource role classes are created using the JAX-RS runtime and MUST have an open constructor for which the JAX-RS runtime can provide all parameter values. Note that the null argument constructor is valid according to this rule.

JAX-RS (POJO @Path), . , .

+2

@Path . @Path, @GET, @POST, @PUT, @HEAD .. @GET .

"time" :

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

"" :

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
        }
    }       
}

, , . , , .

+1

All Articles