users@jersey.java.net

[Jersey] Custom annotations on REST interfaces

From: Veit Guna <Veit.Guna_at_gmx.de>
Date: Wed, 15 Apr 2015 17:11:55 +0200
Hi.
 
I'm using Jersey 2.17 to implement REST services where annotations are put on the interface instead of the resource class.
My interface returns concrete entity types rather than generic Response types, since in my opinion, this is a cleaner approach.
Also this allows to auto-create a typed Apache CXF client based on the interface.
 
To be able to e.g. set the HTTP status code (e.g. 201 instead of default 200 on POST) I would like to use my own annotations
in combination with a ContainerResponseFilter that evaluates the annotations via ContainerResponseContext.getEntityAnnotations():
 
@POST
@HttpStatus(Status.CREATED)
MyEntity create(MyEntity);
 
This works, if I put the annotation on the resource class, but does NOT work when I put the annotation on the interface instead.
Is this a known limitation? Are there plans to enhance that behavior? Since all other annotations (@GET, @Produces etc.) work
on interfaces as well.
 
Another thing are proxied REST endpoints.
 
Background:
I use Spring-Security and tried to secure some REST methods. It works so far. But again, my custom annotations are lost due to
the fact, that JDK-proxies are used by Spring-Security. Using cglib, forces me to put my annotations on the class instead of the
interface that I don't want. So one can get the interface annotations only, when the proxy is unwrapped and the underlying object
is checked.
 
So, the only way I can think of is that the annotation detection of jersey respects possible proxied REST endpoints and checks
for annotations on the underlying object. So calling ContainerResponseContext.getEntityAnnotations() would return the custom
annotations on the endpoint interface. Are there any plans to support such kind of functionality?
 
Is there another way to achieve what I want?
 
Thanks.
Veit