尝试使用 Jersey 注入自定义上下文时缺少字段依赖项
我有一个自定义上下文:
public class MyContext {
public String doSomething() {...}
}
我创建了一个上下文解析器:
@Provider
public class MyContextResolver implements ContextResolver<MyContext> {
public MyContext getContext(Class<?> type) {
return new MyContext();
}
}
现在在资源中我尝试注入它:
@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 上失败。
任何想法将不胜感激。
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.
Any ideas will be appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
JAX-RS 规范不强制 Apache 提供的行为
眨眼。 IOW,您尝试使用的功能适用于 Apache Wink
使您的代码不可移植。
要生成 100% JAX-RS 可移植代码,您需要注入
javax.ws.rs.ext.Providers 实例,然后使用:
检索您的 MyContext 实例。
在Jersey中,还可以直接注入ContextResolver,
这可以为您节省上面一行代码,但请注意,这
策略也不是 100% 可移植的。
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.
To produce 100% JAX-RS portable code, you need to inject
javax.ws.rs.ext.Providers instance and then use:
to retrieve your MyContext instance.
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.
实现 InjectableProvider 。最有可能的是通过扩展 PerRequestTypeInjectableProvider 或 SingletonTypeInjectableProvider。
会让你拥有:
Implement a InjectableProvider. Most likely by extending PerRequestTypeInjectableProvider or SingletonTypeInjectableProvider.
Would let you have: