我有一个类,其中只有静态方法可以通过@path注释访问,并且没有公共构造函数。我简化的程序是:
@Path("")
static class MyStaticClass
{
private MyStaticClass() {...}
@Get @Path("time")
static public String time()
{
return Instant.now().toString();
}
}运行和调用"time“会给出以下错误:
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.发布于 2014-10-30 01:57:18
对不起,根据JSR第3.1.2段
根资源类由JAX运行时实例化,并且必须具有一个公共构造函数,JAX运行库可以为此提供所有参数值。请注意,在此规则下允许使用零参数构造函数。
您可以使用适配器设计模式并创建JAX资源(POJO @Path),它简单地委托给您的静态类。这对于那些在你身后的人来说是很容易理解的。
发布于 2014-10-30 02:04:10
@Path注释旨在定义类级别的资源。执行的方法不是由“Path”控制的,而是由“GET”、“POST”、“PUT”、“HEAD”等控制的。在您的情况下,使用@GET作为所需的操作。
您的“时间”资源类应该如下所示:
@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
}
}
}虽然我不知道这是否管用,但你还是得试试。不过,我不认为把他们都安排在这样的班级里没有多大好处。
https://stackoverflow.com/questions/26549005
复制相似问题