我创建了一个具有多个子资源的 JAX-RS 服务 (MyService),每个子资源都是 MySubResource 的子类。选择的子资源类是根据 MyService 路径中给定的参数选择的,例如:
@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);
}
}
其中 MyPlacesSubResource 和 MyPeopleSubResource 都是 MySubResource 的子类。
MySubResource 定义为:
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();
}
}
结果由相应的 MessageBodyWriters 处理,具体取决于响应的 mimetype。
虽然这可行,但它会导致像 /people/Bob/html 或 /people/Bob/xml 这样的路径,我真正想要的是 /people/Bob.html 或 /people/Bob.xml
有人知道如何完成我想做的事情吗?