This question has been asked several times before , however the answers do not seem to work, and / or Jersey moved on with new changes.
I am looking through some REST APIs using JAX-RS and Jersey (version 2.24). And I want to annotate the interface with JAX-RS and the specific implementation (without any annotations). However, since this patch , Jersey has ceased to support this opportunity. As far as I understand spec , it does not strictly prohibit doing this.
If a subclass or implementation method has any JAX-RS annotations, then all annotations in the superclass or interface method are ignored.
implying that this is quite normal. In many cases, it is useful to use an interface, and each server and client has their respective implementations.
There are many solutions
- Use
ResourceConfig and execute registerClasses(MyImplementation.class) . However, this does not work. - Disable the package scan configuration in
web.xml , create a custom javax.ws.rs.Application and execute the register your implementation. Does not work. - use
ResourceConfig and define a custom AbstractBinder and do a bind so that Jersey dependency injection can find specific implementations. Does not work. - Use RESTEasy. RESTEasy does not seem to introduce interface restrictions, as in Jersey. Never tried it myself.
I would appreciate it if someone could share their experience with this. Any help on getting a Jersey job would be great too. As for option (4), do I really need to switch? 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"; } }
There is also web.xml in which packet scanning is allowed to scan com.foo
source share