我有一个类,只能通过以下方式访问其中的静态方法@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.
抱歉,根据JSR http://download.oracle.com/otndocs/jcp/jaxrs-2_0_rev_A-mrel-eval-spec/index.html,第 3.1.2 段
根资源类由 JAX-RS 运行时实例化并且必须
有一个公共构造函数,JAX-RS 运行时可以为其提供所有
参数值。请注意,零参数构造函数是允许的
在此规则下。
您可以使用适配器设计模式 http://en.wikipedia.org/wiki/Adapter_pattern并创建 JAX-RS 资源(带有 @Path 的 POJO),它只是委托给您的静态类。对于后面的人来说,这很容易理解。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)