球衣2. *。如何替换InjectableProvider和AbstractHttpContextInjectable的Jersey 1. * [英] Jersey 2.*. How to replace InjectableProvider and AbstractHttpContextInjectable of Jersey 1.*

查看:484
本文介绍了球衣2. *。如何替换InjectableProvider和AbstractHttpContextInjectable的Jersey 1. *的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个类,其对象可以使用 @Context 注释注入(或者更好的是自定义注释,我需要传递一个参数的情况注释)转化为资源方法。在泽西岛1. *我将使用 InjectableProvider (在我的情况下与 AbstractHttpContextInjectable )。我想要实现的是像 @Auth [ 1 ]从 dropwizard (使用泽西1.7)。

I would like to create a class whose objects can be injected using the @Context annotation (or better yet a custom annotation for cases where I need to pass an argument to the annotation) into resource methods. In Jersey 1.* I would have used InjectableProvider (in my case together with AbstractHttpContextInjectable). What I'm trying to achieve is something like @Auth [1] from dropwizard (which uses Jersey 1.7).

据了解,泽西岛的注射能力取而代之的是HK2,我找不到任何我所描述的例子。

The injection capabilities of Jersey were replaced by HK2 as far as I know and I could not find any example of what I'm describing.

编辑:请参阅这个问题,我试图遵循Michal指南时遇到的更多问题。

See this question for further problems I have encountered while trying to follow Michal's guide.

推荐答案

您需要从HK2实施 InjectionResolver< T> 界面。看看Jersey工作区中现有的实现:

You need to implement InjectionResolver<T> interface from HK2. Take a look at existing implementations that are present in Jersey workspace:

  • ContextInjectionResolver handling @Context
  • ParamInjectionResolver handling @PathParam, @QueryParam, ... (via it's subclasses)
  • AutowiredInjectResolver handling @Autowired

一旦你有了这个,你需要扩展 AbstractBinder f rom HK2,并通过 #configure()方法绑定您的 InjectionResolver :$ /

Once you have this, you need to extend AbstractBinder from HK2 and bind your InjectionResolver via it's #configure() method:

public class MyResolverBinder extends AbstractBinder {

    @Override
    protected void configure() {
        bind(MyInjectionResolver.class)
                .to(new TypeLiteral<InjectionResolver<MyAnnotation>>() {})
                .in(Singleton.class);
    }
}

...并注册此活页夹的实例您的应用程序类(或通过功能):

... and register an instance of this binder in your application class (or via feature):

功能

public class MyFeature implements Feature {

    @Override
    public boolean configure(final FeatureContext context) {
        context.register(new MyResolverBinder());
        return true;
    }
}

注册 MyFeature 应用程序

public class JaxRsApplication extends Application {

    @Override
    public Set<Class<?>> getClasses() {
        final HashSet<Class<?>> classes = new HashSet<Class<?>>();
        classes.add(MyFeature.class);
        // Register other providers or resources.
        return classes;
    }
}

注册 MyResolverBinder 功能 ResourceConfig

new ResourceConfig()
        // Register either MyFeature
        .register(MyFeature.class)
        // or MyResolverBinder
        .register(new MyResolverBinder())
        // Register other providers or resources
        .packages("my.package");

这篇关于球衣2. *。如何替换InjectableProvider和AbstractHttpContextInjectable的Jersey 1. *的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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