我最近已经开始寻找到Java Web服务,发现以下令人费解:Java的继承注解在接口
如果非要在其中有一个@Consumes注释,然后我实现接口接口中定义的方法中,服务正常工作,就好像@Consumes是继承的。
但是,从阅读各种文章和here,它似乎注释不被继承。
我敲了下面的测试来检查了这一点:
interface ITestAnnotationInheritance {
@Consumes
void test();
}
class TestAnnotationInheritanceImpl implements ITestAnnotationInheritance {
@Override
//@Consumes // This doesn't appear to be inherited from interface
public void test() {}
public static void main(String[] args) throws SecurityException, NoSuchMethodException {
System.out.println(TestAnnotationInheritanceImpl.class.getMethod("test").getAnnotation(Consumes.class));
}
}
,其结果是:
null
如果我取消了@Consumes在TestAnnotationInheritanceImpl类是输出为:
@javax.ws.rs.Consumes(value=[*/*])
这证明注释不是被继承的,但是Web服务如何实现w orks罚款?
非常感谢
'@ Inherited'仅适用于类级别的注释,而不适用于方法级别的注解。 –