Jersey:无法注入自定义参数注释

时间:2014-08-01 07:43:27

标签: java dependency-injection jersey jax-rs hk2

我创建了一个自定义注释,将查询参数注入到我的JAXB生成的类中:

public final class ProjectsQueryParamValueFactoryProvider extends AbstractValueFactoryProvider {

    @Inject
    private ServiceLocator locator;

    @Singleton
    static final class InjectionResolver extends ParamInjectionResolver<ProjectsQueryParam> {

        public InjectionResolver() {
            super(ProjectsQueryParamValueFactoryProvider.class);
        }
    }

    private final static class ProjectsQueryParamValueFactory extends
            AbstractContainerRequestValueFactory<ProjectsQuery> {

        private final ServiceLocator locator;
        private final Parameter parameter;

        private ProjectsQueryParamValueFactory(ServiceLocator locator, Parameter parameter) {
            this.locator = locator;
            this.parameter = parameter;
        }

        @Override
        public ProjectsQuery provide() {
            // TODO Auto-generated method stub
            return new ProjectsQuery();
        }

    }

    @Inject
    public ProjectsQueryParamValueFactoryProvider(MultivaluedParameterExtractorProvider mpep,
            ServiceLocator locator, Source[] compatibleSources) {
        super(mpep, locator, compatibleSources);
    }

    @Override
    public Factory<?> createValueFactory(Parameter parameter) {
        Class<?> classType = parameter.getRawType();

        if (classType == null || (!classType.equals(ProjectsQuery.class))) {
            return null;
        }

        return new ProjectsQueryParamValueFactory(locator, parameter);
    }

}

添加了活页夹:

public class ProjectsQueryParamBinder extends AbstractBinder {

    @Override
    protected void configure() {
        bind(ProjectsQueryParamValueFactoryProvider.class).to(ValueFactoryProvider.class).in(
                Singleton.class);

        bind(ProjectsQueryParamValueFactoryProvider.InjectionResolver.class).to(
                new TypeLiteral<InjectionResolver<ProjectsQueryParam>>() {
                }).in(Singleton.class);
        bind(Parameter.Source.class).to(Parameter.Source.class);
    }

}

并在我的应用中注册:

public MyApplication(){
    packages("org.glassfish.jersey.server.spring", "my.package");
    register(new ProjectsQueryParamBinder());
}

不幸的是,HK2给了我:

org.glassfish.hk2.api.UnsatisfiedDependencyException: There was no object available for injection at Injectee(requiredType=Parameter$Source;,parent=ProjectsQueryParamValueFactoryProvider,qualifiers={}),position=2,optional=false,self=false,unqualified=null,4924537)
at org.jvnet.hk2.internal.ThreeThirtyResolver.resolve(ThreeThirtyResolver.java:74)

web.xml

<servlet>
    <servlet-name>jaxrs-servlet</servlet-name>
    <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
    <init-param>
        <param-name>javax.ws.rs.Application</param-name>
        <param-value>com.MyApplication</param-value>
    </init-param>
    <load-on-startup>2</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>jaxrs-servlet</servlet-name>
    <url-pattern>/rest/*</url-pattern>
</servlet-mapping>

我已经阅读了泽西岛提供的宣传的源代码,它们看起来都像我的代码。粘合剂也一样。错误在哪里?我很困惑,迷失在这里。

我也看过thisthat,但没有用。

0 个答案:

没有答案