简体   繁体   English

如何使用 Jersey 将依赖项注入资源?

[英]How do I inject dependencies into resources with Jersey?

My initial look at Jersey suggested this was a nice framework that made it easy to create RESTful Java APIs.我对 Jersey 的初步了解表明这是一个很好的框架,可以轻松创建 RESTful Java API。

Unfortunately, since making the decision to use it (together with Grizzly), I've found it to be very poorly documented, consequently it is very hard to perform common tasks (that I'm sure are very easy once you know how).不幸的是,自从决定使用它(与 Grizzly 一起)以来,我发现它的文档记录非常差,因此很难执行常见的任务(我相信一旦你知道怎么做就很容易了)。

Anyway, griping over (sorry, but I've wasted a lot of time on this and I'm frustrated), my question:无论如何,抱怨(对不起,但我在这方面浪费了很多时间,我很沮丧),我的问题:

How do I inject objects into resources?如何将对象注入资源? I know I need to use Injectable and related classes, but after reading the Javadocs, it is still unclear how to do this.我知道我需要使用 Injectable 和相关类,但是在阅读了 Javadocs 之后,仍然不清楚如何做到这一点。

My use-case is really simple, I have objects that the resources need to access, and I'd really rather avoid using static fields to expose the objects to the resources.我的用例非常简单,我有资源需要访问的对象,我真的宁愿避免使用 static 字段将对象公开给资源。 Its hard to imagine that anyone has built a Jersey app without doing this, yet I'm having a really hard time finding out how.很难想象有人在不这样做的情况下构建了 Jersey 应用程序,但我很难找出方法。

Can anyone point me to some simple examples of how to do this?谁能指出一些简单的例子来说明如何做到这一点?

PS. PS。 I'd prefer not to use Guice, that seems like overkill for my needs.我不想使用 Guice,这对我的需求来说似乎有点过分了。

I'm not sure what you mean by "resources".我不确定您所说的“资源”是什么意思。

If you're talking about the @Resource annotation, it seems that you have to do theheavy lifting yourself.如果您在谈论 @Resource 注释,似乎您必须自己做繁重的工作。

I eventually gave up on Jersey and switched (back) to Restlet.我最终放弃了 Jersey 并切换(返回)到 Restlet。 Jersey is poorly documented, and convoluted:-( Jersey 的文档记录很差,而且令人费解:-(

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM