java - 如何在 Jersey 中注册一个静态类?

标签 java jersey

我有一个类,其中只能通过 @path 注释访问静态方法,并且没有公共(public)构造函数。我的简化程序是:

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

运行并调用“时间”给我以下错误:

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 , 第 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.

您可以使用 Adapter design pattern并创建 JAX-RS 资源(带有@Path 的 POJO),它只是委托(delegate)给您的静态类。这对于后面的人来说很容易理解。

关于java - 如何在 Jersey 中注册一个静态类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26549005/

相关文章:

java - 如何将客户端发送的Base64图像存储在BlobStore中

java - Jersey :找不到媒体类型 = 应用程序/json、类型 = 类 org.codehaus.jackson.node.ObjectNode 的 MessageBodyWriter?

java - 如何迭代一堆流并一次读取一个元素?

java - 无法在 Ubuntu 18.04 上重新安装 Cassandra

Java DateTimeFormatter 用偏移量解析 YearMonth

java - tomcat 的 Jersey 休息服务无法在 web.xml 中使用欢迎文件配置

java - 在 package-info.java 中使用 JavaDoc 的导入

java - Jboss 无法实例化类“org.jboss.logmanager.handlers.PeriodicRotatingFileHandle

java - 在同一 Jersey 服务中解码不同对象

java - Jersey/JAXB : Use same POJO for HTTP POST and GET, 但在 JSON 响应中仅返回 GET 属性的子集。