球衣中的注解继承 [英] Annotation Inheritance in jersey

查看:29
本文介绍了球衣中的注解继承的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一些具有相同形式的资源类,所以一个好主意是使用 DRY 并使用继承.所以我创建了一个 RootResource 类并在那里放置了一些方法.我想注释它们,然后在子类中实现它们,但它不起作用!这是一个示例代码:

I'm creating some resource class with same form so a good idea is use DRY and use inheritance. So I've create a RootResource class and put some methods there. I want to annotate them and then implement them in subclass but it doesn't work! Here is a sample code:

public abstract class RootResource {
  @GET
  @Path("/{id: .*}")
  public abstract String getInfo(String uid);
}

@Path("/user")
public class UserResource extends RootResource{
  public String getInfo(@PathParam("id") String uid) {
    System.out.println("Hello!");
  }
}

我使用的是球衣 2.6.任何的想法?谢谢.

I'm using jersey 2.6. Any Idea? Thanks.

推荐答案

我在使用 Jersey 时遇到了同样的问题.Java EE 标准对于 JAX-RS 声明如下:

I've been through the same issue while using Jersey. The Java EE standard for JAX-RS states the following:

3.6 注解继承

JAX-RS 注释可以用于 > 超类或已实现的方法和方法参数界面.这样的注解被对应的子类继承或实现类方法提供该方法及其参数没有任何自己的 JAX-RS 注释.注释超类优先于已实现接口上的那些.多个定义的冲突注释的优先级实现的接口是特定于实现的.

JAX-RS annotations MAY be used on the methods and method parameters of a > super-class or an implemented interface. Such annotations are inherited by a corresponding sub-class or implementation class method provided that method and its parameters do not have any JAX-RS annotations of its own. Annotations on a super-class take precedence over those on an implemented interface. The precedence over conflicting annotations defined in multiple implemented interfaces is implementation specific.

如果子类或实现方法有任何 JAX-RS 注释,则超类或接口方法上的所有注释都是忽略.

虽然 Jersey 作为参考实现对这个声明非常严格,但 Resteasy 实现更宽松,并没有我的诀窍.

While Jersey as the reference implementation is very strict with this statement, Resteasy implementation is more lenient and did the trick for me.

这篇关于球衣中的注解继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆