Following my first experiments with Jersey I’ve continue my investigation of Jersey and Guice integration. As mentioned in the previous post my research was mainly centered around Paul Sandoz’s post, the Jersey mailing list (nb. unfortunately I could find much), and as the last resort the source code.
For Jersey 1.3 and Guice integration the news are quite good. There is a jersey-guice module that provides pretty much everything that is needed. And there is also a sample project providing the necessary details for setting this integration up, so I’ll not detail it here. Basically all you have to do is:
But you will also want to be aware of the following three things:
@Inject if you also need to pass in any of the JAX-RS @*ParamViewable JSPsWhile point 1 above may be a non-issue to most/some, the other two deserve a bit of explanation/details.
When using the Guice integration provided in Jersey 1.3, Guice is responsible to both create and inject components, resources, and providers. Unfortunately due to they way bindings are defined in Guice, there was no easy way to make it aware of all @*Param values and so even if it is recommended to use constructor based injection, you’ll not be able to to both use @Inject and @*Param in a constructor. Most probably the scenarios where you’ll see is issue are those elements that have a per-request lifecycle. The good news is that there are simple workarounds for it:
@*Param as fields and the constructor as @Inject@Inject dependencies as either fields or methods, and use the constructor parameters to make Jersey pass in *@ParamsThe last point is covered in more detail and this email thread. According to the conclusions in there, you’ll probably need a Guice trunk build to get around this issue.
In case you are stuck with Java 5 for the time being, you’ll have to go back and use Jesey 1.2. Unfortunately this comes with a couple of other bad news:
Unfortunately I’m in this second situation. My machines are still running Java 5 and an upgrade is not foreseeable. There might be a good part in this though: if I’ll find the time I’ll probably try to backport Jersey 1.3 Guice support to 1.2. But there are still some problems with this (license, distribution, etc.) and I’ll need to consult myself with Jersey people.