我怎样才能在泽西岛2.4过滤器资源的注释? [英] How can I get resource annotations in a Jersey 2.4 filter?

查看:165
本文介绍了我怎样才能在泽西岛2.4过滤器资源的注释?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题基本上是一样的,因为这一项:<一href=\"http://stackoverflow.com/questions/8315188/how-can-i-get-resource-annotations-in-a-jersey-containerresponsefilter\">How我可以得到的资源注解在新泽西ContainerResponseFilter 。

My question is essentially the same as this one: How can I get resource annotations in a Jersey ContainerResponseFilter.

但我使用Java 2.4新泽西州和找不到ResourceFilterFactory或ResourceFilter类的任何迹象。文档也没有提到他们。他们有没有被撤销precated或者他们只是非常好隐藏的?如果他们已经去precated,有什么我可以用呢?现在是否有与新泽西州2.4和2.5的方式从ContainerRequestFilter获取资源的注释吗?

But I'm using Java Jersey 2.4 and can't find any sign of the ResourceFilterFactory or ResourceFilter classes. The documentation also doesn't mention them. Have they been deprecated or are they just really well hidden? If they've been deprecated, what can I use instead? Is there now a way with Jersey 2.4 and 2.5 to get the resource annotations from a ContainerRequestFilter?

感谢

推荐答案

如果你想修改的基础上对资源的方法/类可用注释的请求的处理那么我建议你使用的 DynamicFeature 从JAX-RS 2.0。使用 DynamicFeature 是你可以指定特定提供商的资源可用方法的子集。例如,考虑我有这样一个资源类:

If you want to modify processing of a request based on annotations available on a resource method/class then I'd recommend using DynamicFeature from JAX-RS 2.0. Using DynamicFeatures you can assign specific providers for a subset of available resource methods. For example, consider I have a resource class like:

@Path("helloworld")
public class HelloWorldResource {

    @GET
    @Produces("text/plain")
    public String getHello() {
        return "Hello World!";
    }
}

和我想指定<一个href=\"https://jersey.java.net/apidocs/latest/jersey/javax/ws/rs/container/ContainerRequestFilter.html\"相对=nofollow> ContainerRequestFilter 它。我将创建:

And I'd like to assign a ContainerRequestFilter to it. I'll create:

@Provider
public class MyDynamicFeature implements DynamicFeature {

    @Override
    public void configure(final ResourceInfo resourceInfo, final FeatureContext context) {
        if ("HelloWorldResource".equals(resourceInfo.getResourceClass().getSimpleName())
                && "getHello".equals(resourceInfo.getResourceMethod().getName())) {
            context.register(MyContainerRequestFilter.class);
        }
    }
}

和后注册(如果你使用包扫描,那么你并不需要的情况下,注册它有它 @Provider 注释) MyContainerRequestFilter 将与您的资源方法有关。

And after registration (if you're using package scanning then you don't need to register it in case it has @Provider annotation on it) MyContainerRequestFilter will be associated with your resource method.

在另一方面,你可以随时注入 ResourceInfo中加入过滤(它无法与 @ preMatching 被注解),并从中获得注释:

On the other hand you can always inject ResourceInfo in your filter (it can't be annotated with @PreMatching) and obtain the annotations from it:

@Provider
public class MyContainerRequestFilter implements ContainerRequestFilter {

    @Context
    private ResourceInfo resourceInfo;

    @Override
    public void filter(final ContainerRequestContext requestContext) throws IOException {
        resourceInfo.getResourceMethod().getDeclaredAnnotations();
    }
}

这篇关于我怎样才能在泽西岛2.4过滤器资源的注释?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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