I am working on creating a RESTful API using Jersey. I want to implement versioning via the Accept-Header. My resources, say MyResourceV1, MyResourceV2
and SubresourceV1, SubresourceV2
, are separated into different classes. Now I am using the content-type application/vnd.myapp.resource.v1+json
, which works great for regular resources since Jersey recognizes the custom media type.
@GET
@Path("/list")
@Produces("application/vnd.myapp.resource.v1+json")
public List<MyResourceV1> getProjectList() {
//returns JSON
}
Now MyResource implements the sub resource SubResource
@Path("/{resourceId}/subresource/")
@Produces("application/vnd.myapp.subresource.v1+json")
public SubResourceV1 getSubResource() {
return new SubResourceV1();
}
However, Jersey seems to ignore the @Produces
annotation which results in Jersey complaining about multiple resources registered on the same path:
[...] and resource Resource{"/{resourceId}/subresource/", 0 child resources, 0 resource methods, 1 sub-resource locator, 1 method handler classes, 0 method handler instances}, contains sub resource locators on the same path /{resourceId}/subresource/.
Because MyResourceV2
includes the same path:
@Path("/{resourceId}/subresource/")
@Produces("application/vnd.myapp.subresource.v2+json")
public SubResourceV2 getSubResource() {
return new SubResourceV2();
}
The only workaround I see is to include the different versions in the same file via different methods for the respective version. This bloats the code unnecessarily. Is there any way to have my versions seperated into files and still keep my sub resource locators?