Sunday, May 29, 2011

While IoC is a terrific way to promote software modularity, someone still has to write that bootstrap code to connect all the dots. Even though there is abundant information on how to use Guice, Jersey and Jetty respectively in a servlet environment, little is known about using all three together to programmatically configure servlets in Java. Here I will use an example to demonstrate how to write RESTful servlet using all three:

Assemble framework libraries using Maven.

Create a POJO interface and an implementation.

Write a JAX-RS resoure to use Guice constructor injection and JAX-RS annotations.

Next, compose POJO bindins in a JerseyServletModule. This module will setup the Jersey-based JAX-RS framework for use with Guide injection. The GuiceServletContextListener is used to bootstrap Guice when the servet context is initialized.

10 comments:

No. This is pure Jetty embedding which eschrews web.xml entirely. If you prefer a standard servlet container with web.xml, then swap GuiceLauncher with the exact web.xml fragment as shown here:http://code.google.com/p/google-guice/wiki/Servlets

Any idea how to use the Jersey UriConnegFilter with this stack? My goal is doing context negotiation based on url extension without needing to build something, hence me trying to use this servlet. All the documentation I've found only explains how to use it when you're using a jersey servlet. I tried to chain this guy to the juice filter, but it's not even actually a filter, despite it's name - doesn't extend javax.servlet.Filter.