尝试向Jersey注入自定义上下文时缺少字段依赖项 [英] Missing dependency for field when trying to inject a custom context with Jersey

查看:145
本文介绍了尝试向Jersey注入自定义上下文时缺少字段依赖项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有自定义上下文:

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;

}

我收到以下错误:

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

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

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

任何想法将不胜感激。

推荐答案

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

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.

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

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

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

检索你的MyContext实例。

to retrieve your MyContext instance.

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

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注入自定义上下文时缺少字段依赖项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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