从Jersey中的URI获取处理程序? [英] Get handler from URI in Jersey?

查看:89
本文介绍了从Jersey中的URI获取处理程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ContainerResponseFilter 里面我想得到处理程序,即 @Path 的类和 @GET / @PUT -annotated方法与我将提供的URL匹配。

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.

示例:

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.

推荐答案

你可以注入 UriInfo ExtendedUriInfo UriInfo 仅包含最后匹配的类, ExtendedUriInfo 甚至可以报告匹配的方法(以及更多信息,请参阅链接的javadocs) 。

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).

代码示例:

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

这篇关于从Jersey中的URI获取处理程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆