如何在泽西岛注册静态课程? [英] How to register a static class in Jersey?
问题描述
我有一类只能通过@path
批注访问静态方法的类,并且没有公共构造函数.我的简化程序是:
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.
推荐答案
Sorry, according to the JSR, paragraph 3.1.2
根资源类由JAX-RS运行时实例化,并且必须 有一个公共构造函数,JAX-RS运行时可以为其提供所有 参数值.请注意,零参数构造函数是允许的 在这个规则下.
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.
您可以使用适配器设计模式并创建JAX-RS资源(带有@Path的POJO ),它只是委托给您的静态类.对于那些在你身后的人来说,这很容易理解.
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.
这篇关于如何在泽西岛注册静态课程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!