Get handler from URI in Jersey?

断了今生、忘了曾经 提交于 2019-12-19 03:24:27

问题


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.


回答1:


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;
    }
}


来源:https://stackoverflow.com/questions/7402288/get-handler-from-uri-in-jersey

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!