users@jersey.java.net

[Jersey] Re: Annotations in interfaces, how to use with jersey?

From: Guy Rouillier <guy.rouillier_at_gmail.com>
Date: Sat, 15 Mar 2014 16:48:39 -0400

This actually works out okay. We start by implementing the resource
class, with a complete set of annotations. When we are done, we then
(from within Eclipse) use refactor to extract the interface. When doing
so, refactor includes all the annotations from the implementing class
*except* for the annotations on the class itself (for us, usually just
@Path and @Singleton.) So, we have to remember to copy those manually.
  Would be better, obviously, if Eclipse also generated the class level
annotations, but oh well...

I believe you that having done this, Jersey will then ignore all the
annotations on the interface, because the implementing class has its own
annotations. But it doesn't matter, because the interface and the
implementing class have the exact same set of annotations. The
interface is for use by the client proxy, and the implementing class is
for use by Jersey on the server.

To answer Graham, this arrangement works for us. We have over a dozen
resources implemented with this approach.

On 3/14/2014 2:09 PM, cowwoc wrote:
> No. That's not the case. The way JAX-RS treats annotation inheritance is
> totally screwed up:
> https://jsr311.java.net/nonav/releases/1.0/spec/spec3.html#x3-330003.6
>
> "If a subclass or implementation method has any JAX-RS annotations
> thenallof the annotations on the super class or interface method are
> ignored."
>
> In other words, if you annotate implemented methods with @Override
> (because, uh, we're *supposed* to) or even if you annotate your method
> parameters (think @Context) then JAX-RS will drop all your interface
> annotations. I absolutely hate the way this was designed.
>
> I filed a bug report at https://java.net/jira/browse/JERSEY-1885 but
> based on "Fix Version" it doesn't look like it's going anywhere.
>
> Gili
>
> On 14/03/2014 1:05 PM, Graham Leggett wrote:
>> On 14 Mar 2014, at 6:42 PM, Guy Rouillier<guy.rouillier_at_gmail.com> wrote:
>>
>>> We encountered the same issue. The simplest solution is to move your interfaces to a different package. That was a better solution for us in the long run anyway; we exported the interface package to a client JAR to be used with the client proxy.
>> Am I right in understanding that if jersey finds a concrete class, and the class implements a given interface with the required annotations, jersey will just "do the right thing" and treat the annotations as if they had been applied directly to the concrete class, interface and concrete class just need to be in different packages?
>>
>> Regards,
>> Graham
>> --
>>
>


-- 
Guy Rouillier
---
This email is free from viruses and malware because avast! Antivirus protection is active.
http://www.avast.com