jersey: possible to inherit path from parent class

In the spirit of DRY I’m trying to bundle annotations and shared code that apply to all my REST resources in a central class. Sharing parts of the path does not seem to work.

Let’s say I have two REST resources at /v1/users and /v1/items. The Jersey resources both extend the same parent class V1BaseResource. Can they inherit the v1-part of the path?

Example:

@Path("v1")
@Produces(MediaType.APPLICATION_JSON)   // applies to all child-classes
public class V1BaseResource {

    // maybe even some shared code
}

UsersResource

@Path("users")
public class UsersResource extends V1BaseResource {

    @GET
    public Response getUsers() ...
}

ItemsResource

@Path("items")
public class ItemsResource extends V1BaseResource {

    @GET
    public Response getItems() ...
}

Unfortunately, the @Path annotation of the actual resources overwrites the path, not adds to it.

Is this possible (with out the use of sub-resource locators)?

Answer

If you look at the JSR, it’s not possible :

If a subclass or implementation method has any JAX-RS annotations then all of the annotations on the superclass or interface method are ignored.

Here is the JSR : JSR-339. See Section 3.6.

Leave a Reply

Your email address will not be published. Required fields are marked *