RESTEasy, embedded Jetty, Swagger and Guice

In the previous post, I wrote about the steps I followed when adding support for Swagger and Swagger UI to a RESTful API built using RESTEasy.

In this post, I'll walk you through the steps I followed when adding support for Guice (a lightweight dependency injection framework that implements JSR-330) to a RESTful API built using RESTEasy (packaged in a Fat JAR with an embedded Jetty instance and no web.xml).


  • OpenJDK for Java 1.8
  • Git
  • Maven (I'm using 3.3.9)
  • The Eclipse IDE for Java EE Developers (I'm using Neon)

Note: This post will walk you through the steps required to install the OpenJDK. And, this post will walk you through the steps required to install Git, Maven and the Eclipse IDE.

Getting Started

You've probably heard the expression:

highly cohesive, loosely coupled

When building microservices, dependency injection is one of the tools we can use to help us achieve loose coupling. The most commonly used form of dependency injection is constructor injection and to quote Guice's getting started guide:

To construct an object, you first build its dependencies. But to build each dependency, you need its dependencies, and so on. So when you build an object, you really need to build an object graph.


The first thing we need to do is to update the list of library dependencies in our POM's dependencies element:



  <!-- RESTEasy runtime dependencies -->

  <!-- Guice runtime dependencies -->


We need to include the required Guice libraries as well as the RESTEasy Guice library.


Guice includes servlet extensions that allow us to take advantage of type-safe configuration for our servlet and filter components.

To construct an object Guice needs know what to build, how to resolve dependencies, and how to wire everything together. To specify how dependencies are resolved, we create bindings for our servlets and filters.

We do this by extending Guice's ServletModule class and overriding its configureServlets() method. Let's take a look at the JettyModule:

package org.robferguson.resteasy.examples.fatjar.jetty;


public class JettyModule extends ServletModule {

  protected void configureServlets() {


The JettyModule extends the ServletModule class and overrides its configureServlets() method. In the configureServlets() method we bind Guice’s GuiceFilter class.

In order for Guice to inject dependencies we must route all requests through the GuiceFilter. This filter listens for all requests and if it finds an @Inject annotation it will deliver the appropriate dependencies to that object.

Note: In the Main classes run() method (see below) we will register the Guice Filter with the application's ServletContextHandler instance.

We also need to create bindings for RESTEasy:


public class RestEasyModule extends ServletModule {

  private final String path;

  public RestEasyModule() {
    this.path = null;

  public RestEasyModule(final String path) {
    this.path = path;  // e.g., "/api"

  protected void configureServlets() {


    if (path == null) {
    } else {
      final Map<String, String> initParams =
        ImmutableMap.of("resteasy.servlet.mapping.prefix", path);
      serve(path + "/*").with(HttpServletDispatcher.class, initParams);

In the RestEasyModule’s configureServlets() method we bind the RESTEasy framework’s GuiceResteasyBootstrapServletContextListener and HttpServletDispatcher classes.

GuiceResteasyBootstrapServletContextListener is a helper class that sets up all of RESTEasy's (Guice required) bindings.

And, Swagger:


public class SwaggerModule extends ServletModule {

  private final String path;

  public SwaggerModule() {
    this.path = null;

  public SwaggerModule(final String path) {
    this.path = path;  // e.g., "/api"

  protected void configureServlets() {

    Multibinder<ServletContextListener> multibinder = 
      Multibinder.newSetBinder(binder(), ServletContextListener.class);


    if (path == null) {
    } else {
        filter(path + "/*").through(ApiOriginFilter.class);

In the SwaggerModule’s configureServlets() method we bind the Swagger framework’s ApiListingResource and SwaggerSerializers classes.

We also need to bind our SwaggerServletContextListener class:

final class SwaggerServletContextListener implements 
    ServletContextListener {

  SwaggerServletContextListener() {

  public void contextInitialized(ServletContextEvent event) {

    BeanConfig beanConfig = getBeanConfig();

  private BeanConfig getBeanConfig() {

    BeanConfig beanConfig = new BeanConfig();
    beanConfig.setSchemes(new String[] { "http" });

    beanConfig.setTitle("RESTEasy, Embedded Jetty, Swagger and " 
        + "Guice Example");
    beanConfig.setDescription("Sample RESTful API built using RESTEasy, "
        + "Swagger, Swagger UI and Guice packaged in a Fat JAR with an "
        + "embedded Jetty instance and no web.xml.");

    // setScan() must be called last

    return beanConfig;

  public void contextDestroyed(ServletContextEvent event) {


Modules are the building blocks of an injector (Guice's object-graph builder). Lets create an injector and use it to build our Main class:

public static void main(String[] args) throws Exception {

  try {

    final Injector injector = Guice.createInjector(new JettyModule(),
        new RestEasyModule(APPLICATION_PATH),
        new ResourceModule(), new SwaggerModule(APPLICATION_PATH));


  } catch (Throwable t) {

We also need to update our run() method so that all requests will be routed through the Guice Filter:

public void run() throws Exception {

  final int port = 8080;
  final Server server = new Server(port);

  // Setup the basic Application "context" at "/".
  // This is also known as the handler tree (in Jetty speak).
  final ServletContextHandler context = new ServletContextHandler(server,

  // Add the GuiceFilter
  FilterHolder filterHolder = new FilterHolder(filter);
  context.addFilter(filterHolder, APPLICATION_PATH + "/*", null);

  // Setup the DefaultServlet at "/".
  final ServletHolder defaultServlet = new ServletHolder(
      new DefaultServlet());
  context.addServlet(defaultServlet, CONTEXT_ROOT);

  // Set the path to our static (Swagger UI) resources
  String resourceBasePath = 
  context.setWelcomeFiles(new String[] { "index.html" });

  // Add any Listeners that have been bound
  eventListenerScanner.accept((listener) -> {

  final HandlerCollection handlers = new HandlerCollection();

  // The Application context is currently the server handler,
  // add it to the list.

  // Add any Handlers that have been bound
  handlerScanner.accept((handler) -> {


Build and run the example program

To build the 'Fat JAR Swagger Guice' example (in the examples/fatjar-swagger-guice directory) and run some tests:

mvn clean install

To run the example:

java -jar target/fatjar-swagger-guice-1.0-SNAPSHOT.jar

You should see output like:

19:25:05.748 [main] INFO  org.eclipse.jetty.util.log - 
    Logging initialized @1000ms
19:25:06.041 [main] INFO  org.eclipse.jetty.server.Server - 
19:25:07.450 [main] INFO  org.reflections.Reflections - 
    Reflections took 1281 ms to scan 1 urls, producing ...
19:25:07.979 [main] INFO  o.jboss.resteasy.plugins.guice.i18n - 
    RESTEASY011025: registering provider instance for
19:25:07.980 [main] INFO  o.jboss.resteasy.plugins.guice.i18n -
    RESTEASY011020: registering factory for 
19:25:08.016 [main] INFO  o.jboss.resteasy.plugins.guice.i18n - 
    RESTEASY011020: registering factory for 
19:25:08.075 [main] INFO  o.e.j.server.handler.ContextHandler - Started 
19:25:08.137 [main] INFO  o.e.jetty.server.AbstractConnector - Started
19:25:08.140 [main] INFO  org.eclipse.jetty.server.Server - Started

In your browser navigate to:


You should see output like:

Source Code: