This question had been asked a few times before, however the answers doesn't seem to work and/or Jersey has moved on with more changes.
I'm exposing some REST APIs using JAX-RS and Jersey (version 2.24). And I wish to annotate the interface with JAX-RS and a concrete implementation (without any annotations). However, since this patch Jersey stopped supporting this possibility. As far as I understand the spec, it doesn't strictly prohibit doing that.
If a subclass or implementation method has any JAX-RS annotations then all of the annotations on the superclass or interface method are ignored.
implying that it is totally okay to do that. In many cases it is good to use an interface, and have a server and client each have their respective implementations.
There are plenty of solutions out there,
- Use a
ResourceConfig
and do aregisterClasses(MyImplementation.class)
. However, this doesn't work. - Disable the package scanning configuration in
web.xml
, create a customjavax.ws.rs.Application
and do aregister
of your implementation from there. Doesn't work. - use a
ResourceConfig
and define a customAbstractBinder
and do abind
so that Jersey's dependency injection can find the concrete implementations. Doesn't work. - Use RESTEasy. RESTEasy doesn't seem to impose the interface restrictions as in Jersey. Never tried it myself.
I would appreciate if someone can share their experience with this. Any help on how to get Jersey working would be great too. As for the option (4) is it really necessary to switch ? A sample code below.
MyResource
package com.foo;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
@Path("/hello")
public interface MyResource {
@GET
public String sayHello();
}
MyResourceImpl
package com.bar;
public class MyResourceImpl implements MyResource {
@Override
public String sayHello() {
return "Hello Jersey";
}
}
Also have a web.xml
that has the package scanning enabled to scan com.foo
@Path
on the concrete class instead of interface. This is how I've always known it to work (as described in the JAX-RS spec). I never even knew that interface class anno used to be supported. – Paul Samsotha@Path
annotation on the concrete class. But that defeats the purpose of using an interface in my case. The client and server has to use the same API path. And with your solution I'll end up duplicating the paths for client (my client is a javascript generated code from a swagger spec) – AlavalathiJAX-RS annotations may be used on the methods and method parameters of a super-class or an implemented interface
Note that it says methods only, so class-level annotations on interfaces are implied to be ignored. – Logan Pickup