Intro & Motivation
Following the line with my previous post about Jersey + Guice integration, I wanted to get deeper on the Servlet 3.x specs and the new Asynchronous features in Java.
This was one of the limitations I found in my previous approach. The lack of asynchronous request processing within my Web Application.
My Investigation led me to RESTEasy Framework. I found this framework to be very handy and very updated with Java specs on JAX-RS, JSR-330, and Asynchronous Servlets.
I also bought the book RESTful Java with JAX-RS 2.0 (2nd Edition), which offered a very complete insight about this library and it’s capabilities. It’s also written by Bill Burke, who according to O’Reilly Media “is a Fellow at the JBoss division of Red Hat Inc. A long time JBoss contributor and architect, his current project is RESTEasy, RESTful Web Services for Java”. Big Kudos to Bill Burke for this great book and RESTEasy framework!
Third Approach: Apache Shiro + RESTEasy + Servlet 3 + Guice
This integration was tricky because I had to ditch the so comfortable guice-servlet package. But RESTEasy provides a fancy way to integrate with Guice through their GuiceResteasyBootstrapServletContextListener. Also to complete the whole Servlet/Filter 3 implementation, I used the Filter30Dispatcher.
This third approach also has Apache Shiro integrated, which I explain in detail in other post, to build a fully Secured Web Application.
The ContextListener is the way of RESTEasy to inject Guice’s Injector into the Web Application Context. it has a methods to enumerate all the Modules (Guice AbstractModule’s implementations) needed for the WebApp and a method to operate with configurations just after the Injector is created.
Noteworthy about JAX-RS API
There are several ways to bootstrap your beans/POJOS in your Web Application. The way JAX-RS proposes to expose its Resource Classes is by its Application class. This class defines the Singleton classes and Prototype classes that will have the RESTful annotations for exposing Resources. It is a good practice to have all the RESTful Endpoints in a dedicated package like “.rest” or “.endpoint”, just like with the Jersey framework and their “packages” parameter, so in my case, migrating a Jersey Resource to a RESTEasy one was a very light task if not a massive file copy&paste among projects.
The github project shiro-guice-async-webapp has the sample webapp. It has the following frameworks integrated:
- Servlet 3.1.0 (http://java.net/projects/servlet-spec/pages/Home)
- JBoss RestEasy 3.0.9.Final (http://resteasy.jboss.org)
- Guice 3.0 (http://github.com/google/guice)
- Apache Shiro 1.2.3 (http://shiro.apache.org)
- gson 2.2.4 (http://code.google.com/p/google-gson/)
- joda-time 2.4 (http://www.joda.org/joda-time/)
- JUnit 4.10 (http://junit.org/)
- Jukito 1.4 (http://github.com/ArcBees/Jukito)
- Logback + SLF4J XLogger (http://logback.qos.ch)
Again, this project can be turned into a Maven Archetype, with the mvn command.
Integrate Google Guice with Netty!