@Path jersey for multiple / single REST nouns in the same class

I have a class that annotates using @Path as follows:

 @Path("widgets") @Produces(MediaType.APPLICATION_XML) public class WidgetResource { @GET public Response getWidgets(@QueryParam("limit")) { //This class returns the plural noun, a list of widgets //...} @GET @Path("widget/{id}") public Response getWidgetById(@PathParam("id") long id) { //This class returns a single widget by id //...} 

When I run the test client, localhost / widgets displays as expected, but when the getWidgetById method maps to localhost/widgets/widget/{id} . This is not what I want - I would like to have localhost/widgets and localhost/widget/{id}

I tried to exclude @Path annotation at the class level, but this prevents Jersey from recognizing this class as a REST resource (I tried both ScanningResourceConfig and ClassNameResourceConfig ) - both could not load the class as a resource if there was no @Path at the class level).

I assume that a (ugly) workaround would be to split the methods between the classes of the WidgetResource and WidgetsResource . I think this is a terrible decision, since both of these methods share resources in the same class, but I really need REST-ful localhost/widget (for one object) and localhost/widgets (for plural).

I missed something - do I have a way for Jersey to take the class as a resource class if @Path simply annotates the methods (I couldnโ€™t get it working), if I canโ€™t get the absolute display ( @Path(/widget/{id}) ) or some relative mapping ( @Path(../widget/id ) - none of these works are really just an analogy of what I need. Thanks!

+7
source share
2 answers

This part is about what you need:

Personally, I find your image strange and confusing. Just save it like this:

 @Path("widgets") @Produces(MediaType.APPLICATION_XML) public class WidgetResource { @GET public Response getWidgets(@QueryParam("limit")) { //This method returns the plural noun, a list of widgets // it also possible to limit the number returned by // using a query parameter. You could easily implement // pagination by adding further query parameters like // 'offset', 'sortOrder', etc. //... } @GET @Path("{id}") public Response getWidgetById(@PathParam("id") long id) { //This method returns a single widget by id //... } } 

It seems natural to add a collection path with an identifier to retrieve an object from the collection. No need to do this widgets/widget/{id} . The widget part is obvious and not needed.

Here is a very neat RESTful API tutorial: "Train your REST dog" by apigee I think this is a really good video. The authors make a couple of good points. And here is a link to a longer version of the same presentation


This part is about what you want:

If you really want to maintain multiple / singular dualism (which I really don't recommend), you can annotate your code as follows: But it is really ugly

 @Path("/") @Produces(MediaType.APPLICATION_XML) public class WidgetResource { @GET @Path("widgets") public Response getWidgets(@QueryParam("limit")) { //This method returns the plural noun, a list of widgets //...} @GET @Path("widget/{id}") public Response getWidgetById(@PathParam("id") long id) { //This method returns a single widget by id //... } } 
+8
source

My suggestion is that your paths be as follows: "widgets" and "widgets/id/{id}" . Or, if you knew that you would never ask for anything other than id, your second might simply be "widgets/{id}" .

I would not switch between the plural and the only one in your path. Since you are accessing the same type of resources for both, your root should be the same. The second form simply indicates it more - based on a vector approach to get more specific.

+2
source

All Articles