I have recently started looking into Java Web Services and found the following puzzling:
If I have an method defined in a interface which has an @Consumes annotation and I then implement the interface, the service works correctly, as if the @Consumes is inherited.
However, from reading various articles and here, it appears annotations are not inherited.
I knocked up the following test to check this out:
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));
}
}
and the result is:
null
If I uncomment the @Consumes in the TestAnnotationInheritanceImpl class is the output is:
@javax.ws.rs.Consumes(value=[*/*])
This proves that annotations are not inherited, but how come the web services works fine?
Many Thanks