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

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

问题描述

我正在创建一些具有相同表单的资源类,所以一个好主意是使用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注释然后超级类或接口方法上的所有注释都是
忽略

而泽西岛由于参考实现对此声明非常严格, 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天全站免登陆