问题描述
从我在网上看到的情况来看,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在遭受了大量的样板(手动获取和调用注入器) )。我希望绑定和注入应该通过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的文章给了我希望,因为它更新(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.
诱人的
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服务的实用建议的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!