Intro & Motivation
I started my journey into the depths of Google Guice last year during an Android Coursera MOOC, it was really interesting and I really enjoyed using this Framework for the MOOC.
After my previous encounters with Spring Framework (and Spring Boot features), I wanted to give another DI Framework a try, so I could learn more about this fabulous and helpful design pattern.
I wanted to build the ultimate Java Web Application skeleton, having all the necessary frameworks to start coding RESTful web services for my current projects at work and at the same time keep the integrations as simple as possible, making the application extendable without loosing common features.
I started with the standards. I found a lot of blogs and code about Jersey framework, which was developed by the Oracle guys, so I gave it a try. My experience with Jersey was very successful, in fact I had 4 projects in one year involving this framework.
My Main motivation on starting these integrations was to have a way to port all my coding experience and algorithms across different projects, with the less amount of changes as possible and leveraging all the power of the Java language and its annotations.
First Approach: Jersey + Guice
My first approach was an integration of Jersey 1.x with Google Guice 3.0.
The main benefit from this approach, was that I found the code very portable and lightweight. I didn’t know when CDI and Oracle’s implementation of JSR-330 was going to get viral, so I decided to use Google Guice’s
@Provides annotation, but the code is portable enough to use javax’s implementation of @Inject. In this page you can find Google Guice’s doc on JSR-330 and the key differences with Oracle’s Java implementation.
You can check my sample project in GitHub: jersey-guice-bootstrap
As I stated before, this kind of projects were very demanded, so I decided to do a Maven Archetype. This way I could automate my project bootstrapping.
Automate The Bootstrap Process: Maven Archetype
The Archetype is jersey-guice-webapp-archetype and it has the following frameworks integrated:
- Servlet 2.5 provided by Guice-Servlet package
- Jersey 1.18.1 (Update: the last 1.x version is 1.19)
- Guice 3.0 (Update: The last version is Guice 4)
- guice-persist 3.0
- gson 2.2.4
- joda-time 2.4
- JUnit 4.10
With this architecture you can stay in the Back-End world with JAX-RS Compliant Endpoints, integrate more frameworks like Hibernate with Guice Persist, use GSON to serialize back and forth POJOS without touching the Jersey’s Jackson configuration. Also this allows you to host your Front-End frameworks in another server or package system, which is a pretty common practice nowadays (MBaaS and SaaS architectures) to decouple Back-End and Front-End. This last also helps when having distributed teams, and their responsibilities.
This was my first Adventure with Google Guice and Java Web Applications, but not the last.
In fact this is going to be the first post in a series of experiences I earned last year. Stay tuned for more Guice Integration Adventures.
Next Adventure: RESTEasy + Servlet 3.x
The next posts will feature Servlet 3.x Spec and RESTEasy implementation of JAX-RS library.