【发布时间】:2015-05-22 15:20:24
【问题描述】:
您好,我正在使用 dropwizard 构建一个应用程序,它在内部使用 jersey 2.16 作为 REST API 框架。
对于所有资源方法的整个应用程序,我需要一些信息,以便解析这些信息,我定义了一个自定义过滤器,如下所示
@java.lang.annotation.Target(ElementType.PARAMETER)
@java.lang.annotation.Retention(RetentionPolicy.RUNTIME)
public @interface TenantParam {
}
租户工厂定义如下
public class TenantFactory implements Factory<Tenant> {
private final HttpServletRequest request;
private final ApiConfiguration apiConfiguration;
@Inject
public TenantFactory(HttpServletRequest request, @Named(ApiConfiguration.NAMED_BINDING) ApiConfiguration apiConfiguration) {
this.request = request;
this.apiConfiguration = apiConfiguration;
}
@Override
public Tenant provide() {
return null;
}
@Override
public void dispose(Tenant tenant) {
}
}
我还没有真正实现该方法,但结构在上面。还有一个 TenantparamResolver
public class TenantParamResolver implements InjectionResolver<TenantParam> {
@Inject
@Named(InjectionResolver.SYSTEM_RESOLVER_NAME)
private InjectionResolver<Inject> systemInjectionResolver;
@Override
public Object resolve(Injectee injectee, ServiceHandle<?> serviceHandle) {
if(Tenant.class == injectee.getRequiredType()) {
return systemInjectionResolver.resolve(injectee, serviceHandle);
}
return null;
}
@Override
public boolean isConstructorParameterIndicator() {
return false;
}
@Override
public boolean isMethodParameterIndicator() {
return true;
}
}
现在在我的资源方法中,我正在执行如下操作
@POST
@Timed
public ApiResponse create(User user, @TenantParam Tenant tenant) {
System.out.println("resource method invoked. calling service method");
System.out.println("service class" + this.service.getClass().toString());
//DatabaseResult<User> result = this.service.insert(user, tenant);
//return ApiResponse.buildWithPayload(new Payload<User>().addObjects(result.getResults()));
return null;
}
这是我配置应用程序的方式
@Override
public void run(Configuration configuration, Environment environment) throws Exception {
// bind auth and token param annotations
environment.jersey().register(new AbstractBinder() {
@Override
protected void configure() {
bindFactory(TenantFactory.class).to(Tenant.class);
bind(TenantParamResolver.class)
.to(new TypeLiteral<InjectionResolver<TenantParam>>() {})
.in(Singleton.class);
}
});
}
问题是在应用程序启动过程中出现以下错误
WARNING: No injection source found for a parameter of type public void com.proretention.commons.auth.resources.Users.create(com.proretention.commons.api.core.Tenant,com.proretention.commons.auth.model.User) at index 0.
还有很长的栈错误栈和描述
以下是用户pojo的声明签名
公共类用户扩展 com.company.models.Model {
用户类上没有注释。 Model 是一个只定义一个 long 类型的属性 id 并且在模型类上也没有注释的类
当我从上面的创建资源方法中删除 User 参数时,它可以正常工作,当我删除 TenantParam 时,它也可以正常工作。仅当我同时使用 User 和 TenantParam 时才会出现此问题
- 我在这里缺少什么?如何解决此错误?
已编辑
我刚刚尝试了两个自定义方法参数注入,也不起作用
@POST
@Path("/login")
@Timed
public void validateUser(@AuthParam AuthToken token, @TenantParam Tenant tenant) {
}
- 我在这里缺少什么?这是对球衣的限制吗?
【问题讨论】:
-
你知道为什么它不适用于方法参数
标签: java rest jersey-2.0 dropwizard hk2