我创建了一个自定义注释来将查询参数注入到我的 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>
我已经阅读了泽西提供的注释的源代码,它们看起来都像我的代码。装订器也是如此。错误在哪里?我很困惑,迷失在这里。