If I am given a relative path (say path = "/foo/1/bar"
), how can I find out which resource's method will be used to treat the request (this would be FooResource.bar
method in the current example)?
I have tried to use Reflections, by looking at all the @Path
annotated classes and methods, and build a map to point back to those methods. However, that would not be ideal, since I'll have to test the given relative path against all existing @Path
s.
Here is an example Resource:
@Path("/foo")
public class FooResource {
@GET
@Path("{id}/bar")
public String bar(@PathParam("id") int barId)
{
return "Hello Bar " + Integer(barId).toString();
}
@POST
@Path("/biz")
public String biz(BisRequest biz)
{
return "Hello Biz";
}
}
Instead of my current solution, which is:
mapper = new HashMap<String, Method>();
/** content of mapper
String "/foo/{id}/bar" -> java.lang.reflect.Method FooResource.bar
String "/foo/biz" -> java.lang.reflect.Method FooResource.biz
*/
public Method findResourceMethodForPath(String path, HashMap<String, Method> mapper) {
String correctKey = findCorrectKeyForPath(path, mapper.keySet());
return mapper.get(correctKey);
}
Would there be a cleaner way to implement findResourceMethodForPath
without having to use the mapper
, as defined in the snippet above?