In my current architecture, I have a JAX-RS resource that sits behind:
/categories
/categories/{catId}
that's implemented like this:
@Path("/categories")
@Produces("application/json")
public class CategoryResourcesApi {
@GET
public Response getCategories() {
// ...
}
@GET @Path("/{catId}")
public Response getCategory(@PathParam("catId") String catId) {
// ...
}
// ...
}
and another one that serves:
/products
/products/{prodId}
and has a similar implementation:
@Path("/products")
@Produces("application/json")
public class ProductResourcesApi {
@GET
public Response getProducts() {
// ...
}
// ...
}
Apart from these straightforward paths, I also need to serve these:
/categories/{catId}/products
/categories/{catId}/products/{prodId}
which would be products related to a specific category.
The most natural thing to do would be make ProductResourcesApi
serve them, but by the way I understand the JAX-RS annotations structure, this can only be served by CategoryResourcesApi
(or eventually by a third class, I think).
I'm using @Context
and other annotations in my resource implementations, so a direct new ProductResourcesAPI().getProducts()
wouldn't work, I guess.
Is there any way to forward from one resource path to another within the JAX-RS (or Jersey) framework? What other options do I have? I'd like to keep all this easily maintainable if possible, that's why I chose one resource for each root path with subresources within.