尝试向Jersey注入自定义上下文时缺少字段依赖项

时间:2021-11-05 19:34:05

I have a custom context:

我有一个自定义上下文:

public class MyContext {
    public String doSomething() {...}
}

I have created a context resolver:

我创建了一个上下文解析器:

@Provider
public class MyContextResolver implements ContextResolver<MyContext> {

     public MyContext getContext(Class<?> type) {
         return new MyContext();
     }
}

Now in the resource I try to inject it:

现在在资源中我尝试注入它:

@Path("/")
public class MyResource {

    @Context MyContext context;

}

And I get the following error:

我收到以下错误:

SEVERE: Missing dependency for field: com.something.MyContext com.something.MyResource.context

The same code works fine with Apache Wink 1.1.3, but fails with Jersey 1.10.

相同的代码在Apache Wink 1.1.3中运行良好,但在Jersey 1.10中失败。

Any ideas will be appreciated.

任何想法将不胜感激。

2 个解决方案

#1


10  

JAX-RS specification does not mandate the behavior provided by Apache Wink. IOW, the feature you are trying to use that works on Apache Wink makes your code non-portable.

JAX-RS规范并未强制要求Apache Wink提供的行为。 IOW,您尝试使用的功能适用于Apache Wink,使您的代码不可移植。

To produce 100% JAX-RS portable code, you need to inject javax.ws.rs.ext.Providers instance and then use:

要生成100%JAX-RS可移植代码,您需要注入javax.ws.rs.ext.Providers实例,然后使用:

ContextResolver<MyContext> r = Providers.getContextResolver(MyContext.class, null);
MyContext ctx = r.getContext(MyContext.class);

to retrieve your MyContext instance.

检索MyContext实例。

In Jersey, you can also directly inject ContextResolver, which saves you one line of code from the above, but note that this strategy is also not 100% portable.

在Jersey中,你也可以直接注入ContextResolver,它可以从上面保存一行代码,但请注意,这种策略也不是100%可移植的。

#2


0  

Implement a InjectableProvider. Most likely by extending PerRequestTypeInjectableProvider or SingletonTypeInjectableProvider.

实现InjectableProvider。最有可能通过扩展PerRequestTypeInjectableProvider或SingletonTypeInjectableProvider。

@Provider
public class MyContextResolver extends SingletonTypeInjectableProvider<Context, MyContext>{
    public MyContextResolver() {
        super(MyContext.class, new MyContext());
    }
}

Would let you have:

让你有:

@Context MyContext context;

#1


10  

JAX-RS specification does not mandate the behavior provided by Apache Wink. IOW, the feature you are trying to use that works on Apache Wink makes your code non-portable.

JAX-RS规范并未强制要求Apache Wink提供的行为。 IOW,您尝试使用的功能适用于Apache Wink,使您的代码不可移植。

To produce 100% JAX-RS portable code, you need to inject javax.ws.rs.ext.Providers instance and then use:

要生成100%JAX-RS可移植代码,您需要注入javax.ws.rs.ext.Providers实例,然后使用:

ContextResolver<MyContext> r = Providers.getContextResolver(MyContext.class, null);
MyContext ctx = r.getContext(MyContext.class);

to retrieve your MyContext instance.

检索MyContext实例。

In Jersey, you can also directly inject ContextResolver, which saves you one line of code from the above, but note that this strategy is also not 100% portable.

在Jersey中,你也可以直接注入ContextResolver,它可以从上面保存一行代码,但请注意,这种策略也不是100%可移植的。

#2


0  

Implement a InjectableProvider. Most likely by extending PerRequestTypeInjectableProvider or SingletonTypeInjectableProvider.

实现InjectableProvider。最有可能通过扩展PerRequestTypeInjectableProvider或SingletonTypeInjectableProvider。

@Provider
public class MyContextResolver extends SingletonTypeInjectableProvider<Context, MyContext>{
    public MyContextResolver() {
        super(MyContext.class, new MyContext());
    }
}

Would let you have:

让你有:

@Context MyContext context;