I've created a JAX-RS service (MyService) that has a number of sub resources, each of which is a subclass of MySubResource. The sub resource class being chosen is picked based on the parameters given in the MyService path, for example:
@Path("/") @Provides({"text/html", "text/xml"})
public class MyResource {
@Path("people/{id}") public MySubResource getPeople(@PathParam("id") String id) {
return new MyPeopleSubResource(id);
}
@Path("places/{id}") public MySubResource getPlaces(@PathParam("id") String id) {
return new MyPlacesSubResource(id);
}
}
where MyPlacesSubResource and MyPeopleSubResource are both sub-classes of MySubResource.
MySubResource is defined as:
public abstract class MySubResource {
protected abstract Results getResults();
@GET public Results get() { return getResults(); }
@GET @Path("xml")
public Response getXml() {
return Response.ok(getResults(), MediaType.TEXT_XML_TYPE).build();
}
@GET @Path("html")
public Response getHtml() {
return Response.ok(getResults(), MediaType.TEXT_HTML_TYPE).build();
}
}
Results is processed by corresponding MessageBodyWriters depending on the mimetype of the response.
While this works it results in paths like /people/Bob/html or /people/Bob/xml where what I really want is /people/Bob.html or /people/Bob.xml
Does anybody know how to accomplish what I want to do?