Reputation: 1019
I have two Jersey methods that look something like this
@GET
@Path("/mine")
@Produces(MediaType.APPLICATION_JSON)
List<MyStuff> getAllMyStuff();
@GET
@Path("/{id}")
@Produces(MediaType.APPLICATION_JSON)
MyStuff getMyStuff(@PathParam("id"));
In this case I can request to /mine and 'getAllMyStuff', or alternatively request /123 and get the right individual stuff. However, I have some optional query params I'd like to use on the 'mine' path, and doing so seems to throw jersey for a loop. When I change 'mine' to be like
@GET
@Path("/mine")
@Produces(MediaType.APPLICATION_JSON)
List<MyStuff> getAllMyStuff(@QueryParam("offset") int offset, @QueryParam("limit") int limit);
calling '/mine' ends up being mapped to the 'getMyStuff' method with an ID of 'mine'.
It seems really strange to me that simply listing those query parameters would affect the mapping like this. Is there any way to work around it?
Upvotes: 0
Views: 278
Reputation: 1019
It turns out that the issue actually has to do with the way I had declared the annotations across the interface and the implementation.
I had an interface with a method like:
@GET
@Path("/mine")
@Produces(MediaType.APPLICATION_JSON)
List<MyStuff> getAllMyStuff(@QueryParam("offset") int offset, @QueryParam("limit") int limit);
and the implementation like
List getAllMyStuff(@QueryParam("offset") int offset, @QueryParam("limit") int limit);
Apparently having any Jersey annotations on the implementing method ends up negating those 'inherited' from the interface. Simply changing my implementation to
List getAllMyStuff(int offset, int limit);
has fixed the problem. Thanks for the help!
Upvotes: 1
Reputation: 657
I believe your issue is that "mine" matches both getMyStuff and getAllMyStuff methods. To disambiguate it, you can either:
@GET
@Path("/")
@Produces(MediaType.APPLICATION_JSON)
List<MyStuff> getAllMyStuff(@QueryParam("offset") int offset, @QueryParam("limit") int limit);
@GET
@Path("/{id}")
@Produces(MediaType.APPLICATION_JSON)
MyStuff getMyStuff(@PathParam("id"));
If you need to keep /mine as a path, you can further specify the valid values for an id so there is no potential for collisions.
For example, if all your id's are numeric:
@GET
@Path("/mine")
@Produces(MediaType.APPLICATION_JSON)
List<MyStuff> getAllMyStuff(@QueryParam("offset") int offset, @QueryParam("limit") int limit);
@GET
@Path("/{id: [0-9]?}")
@Produces(MediaType.APPLICATION_JSON)
MyStuff getMyStuff(@PathParam("id"));
Note: I'm not sure I got the regular expression right.
Upvotes: 0