user23987
user23987

Reputation:

Practical advice on using Jersey and Guice for RESTful service

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:

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

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.

Upvotes: 64

Views: 32916

Answers (11)

bnsd55
bnsd55

Reputation: 318

I created a Guice 4.2, Jetty 9.4 and Jersey 2.7 sample application:

https://github.com/bnsd55/jetty-jersey-guice-starter-kit

As Sunny said:

If you have any questions or suggestions for how to improve the example, feel free to message me via github. The goal is to make this a very accessible introduction to REST on the Java stack.

Upvotes: 0

Alex Ntousias
Alex Ntousias

Reputation: 9132

Although Sunny Gleason's example is great, it is a bit outdated now.

So, after struggling a lot today trying to make Guice and Jersey play nice with each other, I created the following sample project to get you started:

https://github.com/MaliciousMustard/gradle-guice-jersey-jetty

This project is using the following technologies:

  1. Guice for DI
  2. Jersey for the RESTful API
  3. Jackson for POJO to JSON mapping
  4. Jetty for the web-server
  5. Gradle

I guess the most important thing is that you don't have to explicitly specify every new resource class you're adding. As long as you're adding them to the package that is being scanned (look at malicious.mustard.modules.JerseyModule), they will be found automatically!

Upvotes: 3

mckamey
mckamey

Reputation: 17539

These examples were all great starts for me, but I wanted a full MVC stack using Jersey-Guice at it's core. I've been working on refining that for sometime. As of this week this MVC stack is fully deployed to Maven Central repository as an archetype. This means you can now create a new Jersey-Guice stack with one Maven command:

mvn archetype:generate \
    -DarchetypeGroupId=org.duelengine \
    -DarchetypeArtifactId=duel-mvc-archetype \
    -DarchetypeVersion=0.2.1

This automatically generates your own project with you specified package naming so you don't have to manually edit a template project.

See the project Readme.md for more details: https://bitbucket.org/mckamey/duel-mvc

Details on the dual-side views (client-side template & server-side views) I use are here: https://bitbucket.org/mckamey/duel but you could replace with whatever you use.

Upvotes: 1

stickfigure
stickfigure

Reputation: 13556

GWizard includes a module that gives you out-of-the-box integration between Jersey2 and Guice. Here's an example of a complete JAX-RS service:

public class Main {
    @Path("/hello")
    public static class HelloResource {
        @GET
        public String hello() {
            return "hello, world";
        }
    }

    public static class MyModule extends AbstractModule {
        @Override
        protected void configure() {
            bind(HelloResource.class);
        }
    }

    public static void main(String[] args) throws Exception {
        Guice.createInjector(new MyModule(), new JerseyModule()).getInstance(Run.class).start();
    }
}

Note that this is based on the Squarespace jersey2-guice adapter, which may not function properly with future point releases of Jersey. GWizard also offers a RESTEasy JAX-RS module, which is preferred.

Here is a blog entry about this that might help: http://blorn.com/post/107397841765/guice-and-jersey-2-the-easy-way

Upvotes: 2

Gili
Gili

Reputation: 90023

The Jersey-Guice plugin Javadoc provides a pretty good description:

http://jersey.java.net/nonav/apidocs/1.1.5/contribs/jersey-guice/com/sun/jersey/guice/spi/container/servlet/package-summary.html

Upvotes: 1

Nehc
Nehc

Reputation: 760

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

Upvotes: 52

Le Hibou
Le Hibou

Reputation: 1591

I found an interesting project for lightweight Jetty+Guice+Jackson web services: https://github.com/talis/jersey-common/

Upvotes: 0

Daniel Bimschas
Daniel Bimschas

Reputation: 111

Inspired by Sunnys sample application I've created a similar sample project that uses standard WAR files for deployment, e.g. in Apache Tomcat. You can find the project here:

https://github.com/danbim/template-guice-jersey-tomcat

Have fun! Daniel

Upvotes: 9

Scott
Scott

Reputation: 31

I was having similar problems initially trying to use Guice to do constructor injection on my Jersey annotated classes, but eventually got it working, albeit with a fairly trivial application.

I followed the instructions here: jersey-guice javadoc

The trick in my case was that I needed to remove the standard Jersey configuration from my web.xml (like the Jersey ServletContainer) and keep only the Guice listener and Guice filter. Once I did that Guice was being called to create my JAX-RS annotated object, and Jersey was injecting my JAX-RS annotated methods (like @GET, etc.) as expected.

Upvotes: 3

Sunny Gleason
Sunny Gleason

Reputation: 209

I created a Guice/Jersey/Jetty/Jackson sample application here:

http://github.com/sunnygleason/j4-minimal

If you have any questions or suggestions for how to improve the example, feel free to message me via github. The goal is to make this a very accessible introduction to REST on the Java stack.

Hope this helps - have a great day!

-Sunny

Upvotes: 20

Jonas
Jonas

Reputation: 3020

I believe I cannot use constructor injection with any resource as Jersey wants to control this

You cannot use guice's constructor injection because creation of resource is managed by jersey. In this case you can use jersey's @Inject annotation before constructor parameter you want to get injected:

public NewsResource(@Inject NewsService service)

Upvotes: 3

Related Questions