关于使用Jersey和Guice进行RESTful服务的实用建议 [英] Practical advice on using Jersey and Guice for RESTful service

查看:174
本文介绍了关于使用Jersey和Guice进行RESTful服务的实用建议的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从我在网上看到的情况来看,Guice + Jersey整合的最新技术水平自2008年以来一直停滞不前,当时两支球队都陷入了僵局。问题的关键在于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'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的文章使用Jersey,Guice和JSR-250创建RESTful服务给了我希望,因为它更新(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.

诱人的 James Strachan写道:

tantalizingly James Strachan writes:


JAX-RS适用于依赖
注入框架,如Spring,
Guice,GuiceyFruit或JBossMC - 你
可以基本上选择你b $ b更喜欢的那个。

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

但是从实践者的角度来看,我没有看到任何证据。 / p>

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与泽西的整合并没有停滞不前。反之亦然。感谢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

这篇关于关于使用Jersey和Guice进行RESTful服务的实用建议的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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