使用 Jersey 和 Guice 进行 RESTful 服务的实用建议

2022-01-21 00:00:00 java jax-rs guice jersey jsr330

根据我在网上找到的信息,自 2008 年两支球队似乎陷入僵局以来,Guice + Jersey 整合的最新技术一直停滞不前.问题的症结在于 JAX-RS 注释执行字段和方法注入,这与 Guice 自己的依赖注入不能很好地配合.

From what I can find online, the state of the art for Guice + Jersey integration has stagnated since 2008 when it appears both teams reached an impasse. The crux of the issue is that JAX-RS annotations perform field and method injection and this doesn't play nicely with Guice's own dependency injection.

我发现的几个例子还不足以说明:

A few examples which I've found don't go far enough to elucidate:

  • Iqbalyusuf 在 Jersey + Guice on Google 上的帖子App Engine Java 存在大量样板文件(手动获取和调用注入器).我希望绑定和注入应该通过 Guice 注释在幕后进行.

  • Iqbalyusuf's post on Jersey + Guice on Google App Engine Java suffers from a lot of boilerplate (manually getting and calling the injector). I want binding and injection should happen behind the scenes via Guice annotations.

Jonathan Curran 的文章 创建 RESTful 服务使用 Jersey、Guice 和 JSR-250 给了我希望,因为它更新了很多(2010 年),但只是展示了如何在 Guice ServletModule 中启动 Jersey 服务.但是,没有任何真正的依赖注入的例子.我想这留给读者作为练习.Curran 的帖子实际上可能是连接 Guice 和 Jersey 的正确第一步,所以我计划从这个开始.

Jonathan Curran's article Creating a RESTful service with Jersey, Guice, and JSR-250 gave me hope because it's much more current (2010), but went no further than showing how to start up a Jersey service inside of a Guice ServletModule. However, there are no examples of doing any real dependency injection. I suppose that was left as an exercise for the reader. Curran's post may in fact be the correct first step towards wiring up Guice and Jersey and so I plan on starting with that.

诱人的 詹姆斯斯特拉坎写道:

JAX-RS 可以很好地处理依赖关系注入框架,例如 Spring,Guice、GuiceyFruit 或 JBossMC - 你基本上可以随便挑一个喜欢.

JAX-RS works well with dependency injection frameworks such as Spring, Guice, GuiceyFruit or JBossMC - you can basically pick whichever one you prefer.

但从从业者的角度来看,我没有看到任何证据.

But I see no evidence that is true from a practitioner's point of view.

我发现缺少的是关于如何结合 JAX-RS 和 Guice 注释的实际示例和解释.例如:

What I find lacking are practical examples and explanations on how to combine JAX-RS and Guice annotations. For instance:

  • 我相信我不能对任何资源使用构造函数注入,因为 Jersey 想要控制它
  • 我不确定是否可以将@Inject 与@PathParam、@QueryParam 等结合使用.
  • 如何在 MessageBodyWriter 实现中使用注入

有没有人有例子,最好有源代码,结合 Jersey 和 Guice 而不牺牲其中一个或另一个的非平凡应用程序?无论如何,我都会继续走这条路,但泽西和吉斯名单上的点点滴滴让我觉得我在重复其他人的工作.

Does anyone have examples, preferably with source, of non-trivial application which combines Jersey and Guice without sacrificing one or the other in the process? I'm keeping on this road regardless, but the bits and pieces on the Jersey and Guice lists makes me think I'm repeating the work of others who came before me.

推荐答案

Guice 与 Jersey 的集成并没有停滞不前.反之亦然.感谢 Paul 和他在 Jersey 背后的团队,最新的 1.7 版本包含一个特殊的 JerseyServletModule 类,可以与基于 Guice 的 servlet 一起使用.基于 Guice 的构造函数注入 JAX-RS 资源有效! 问题是在 JAX-RS 资源的构造函数中使用了 JAX-RS 注释,例如 @QueryParam.你不需要它!您一直使用 Guice 进行 POJO 注入,包括单例.那么 JAX-RS 只是为解析基于 HTTP 的 RESTful API(如 URL 路径、查询参数、内容类型等)锦上添花.您也不需要工业实力"示例.Guice 和 Jersey 都经过了实战考验.您只需要一个完整的工作示例来了解它是如何工作的.然后,您可以自行试验高级功能.查看以下链接以获取使用 Guice 3.0 和 Jersey 1.7 的完整示例,它们都是最新版本:
http://randomizedsort.blogspot.com/2011/05/using-guice-ified-jersey-in-embedded.html

Guice integration with Jersey has not stagnated. The opposite is true. Thanks to Paul and his cohorts behind Jersey, the latest 1.7 release contains a special JerseyServletModule class to work with Guice-based servlets. Guice-based constructor injection into JAX-RS resource works! The issue is using JAX-RS annotations such as @QueryParam in the constructor of a JAX-RS resource. You don't need it! You use Guice for POJO injection all the way including singletons. Then JAX-RS is just icing on the cake for parsing HTTP-based RESTful APIs such as URL path, query parameters, content-type and etc. You don't need an "industrial strength" example either. Both Guice and Jersey are already battle tested. You just need a complete working example to see how it works. Then you can experiment advanced features on your own. Check out the following link for a complete example using Guice 3.0 and Jersey 1.7, which are all latest releases:
http://randomizedsort.blogspot.com/2011/05/using-guice-ified-jersey-in-embedded.html

相关文章