Sabir Khan Sabir Khan - 1 month ago 6x
reST (reStructuredText) Question

Annotation Processor for Jersey annotations

I am just curious to know about annotation processor for Jersey annotations like for

etc . I mean, would that be provided by
Jersey API
or by

I can't seem to figure out as how these REST annotations get processed and can I find that out by exploring code.

Clicking on
takes me its definition but I need to know the logic that processes this annotation.

I am using Eclipse and project specific annotations are not enabled.

My rest resources work perfectly when deployed in tomcat so that means annotations are getting processed by some processor.


Annotations are easy to process, without the need for any special processor. For example with the @Path, you can simply do something like

public class OrdersResource {}

Path annotationInstance = OrdersResource.class.getAnnotation(Path.class);
String pathValue = annotationInstance.value();

Mainly the thing to take away from this is that generally you get the instance of the annotation from doing some Reflection. Then just call the methods on the annotation to get any values to process, or just check for marker annotations to do some processing.

When Jersey processes the resource classes, it builds an internal model using the annotations. In psuedo-code it might look something like

public class OrdersResource {
    public String get() {}

Path anno = OrdersResource.class.getAnnotation(Path.class);
String path = anno.value();
Resource resource = new Resource(path);

Method[] methods = OrdersResource.class.getDeclaredMethods();
for (Method method: methods) {
    Annotation[] methodAnnos = method.getAnnotations();
    if (arrayContains(methodAnnos, (@GET, @POST, @PUT, etc)) {
        String httpMethod = getMethod(methodAnnos);
        ResourceMethod resourceMethod = new ResourceMethod(httpMethod);
        Produces producesAnno = method.getAnnotation(Produces.class);
        if (produces != null) {

With this resource model, Jersey uses it to process request.

Now the above was all pseudo code. The reflection code is real, but the APIs for the Resource and ResourceMethod are imaginary. But Jersey really has those classes, that it uses to model the resources. For example you can do

Resource resource = Resource.from(OrdersResource.class);

Just like that, we have the model. With the model we can do

String path = resource.getPath();
ResourceMethod method = resource.getResourceMethods();

See Also: Introspecting Jersey resource model Jersey 2.x for some more explanation of how to introspect the model.

As far as the exact implementation of how the annotations are processed when you do say Resource resource = Resource.from(OrdersResource.class). I think that is internal private stuff. I don't know if that is exposed. But it almost surely uses reflection. The only other way would be to introspect the byte code. I don't think that is how it is done (though don't quote me).