Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get handler from URI in Jersey?

Tags:

java

rest

jersey

Inside a ContainerResponseFilter I would like to get the "handler", i.e. the class where @Path and the @GET/@PUT-annotated method matches the URL I will provide.

Example:

someJerseyVariable.getHandlerForURI(request.getRequestUri()); 

I can't find any similar method.

The reason I want this, is to have statistics for how many requests each handler served and how many succeeded/failed. Any other alternatives are also welcome.

like image 769
mbdev Avatar asked Sep 13 '11 13:09

mbdev


1 Answers

You can inject UriInfo or ExtendedUriInfo. UriInfo contains only last matched class, ExtendedUriInfo can even report matched method (and much more info, see the linked javadocs).

Code sample:

public class Filter implements ContainerResponseFilter {
    @Context UriInfo uriInfo;
    @Context ExtendedUriInfo extendedUriInfo;

    @Override
    public ContainerResponse filter(ContainerRequest request, ContainerResponse response) {
        System.out.println(uriInfo.getMatchedResources().get(0).getClass());
        System.out.println(extendedUriInfo.getMatchedMethod().toString());
        return response;
    }
}
like image 188
Pavel Bucek Avatar answered Sep 26 '22 01:09

Pavel Bucek