Guice module for starting Jetty based rest server with Jersey
Switch branches/tags
Nothing to show
Clone or download

README.md

Jersey Guice Module

Build Status Coverage Status Maven Central

Introduction

Jersey comes with its own dependency injection framework for instantiating its classes. If you're using Guice as your dependency injection framework, and you want to inject your own classes into the JAX-RS classes you created - such as Resources and Filters - you need to bridge the gap between the two DI frameworks. This module aims to do just that by booting Jetty based Jersey server and initializing the bridge between HK2 and Guice.

Installation

Gradle

compile 'io.logz:guice-jersey:1.0.5'

Maven

<dependency>
  <groupId>io.logz</groupId>
  <artifactId>guice-jersey</artifactId>
  <version>1.0.5</version>
</dependency>

Usage

Getting Started

  1. Add JerseyModule to your Guice Injector
  2. Configure packages to scan for resources and a port to expose
  3. Get instance of JerseyServer and start consuming your Restful resources
public class Main {
    public static void main(String[] args) throws Exception {
        JerseyConfiguration configuration = JerseyConfiguration.builder()
            .addPackage("com.example.resources")
            .addPort(8080)
            .build();
        
        List<Module> modules = new ArrayList<>();        
        modules.add(new JerseyModule(configuration));
        modules.add(new AbstractModule() {
          @Override
          protected void configure() {
            // Your module bindings ...
          }
        });
        
        Guice.createInjector(modules)
          .getInstance(JerseyServer.class).start();
    }
}

Motivation

Why Jersey?

I was looking for REST library I can drop in place and it will integrate seamlessly with my Guice based project.

My requirements from the rest library were:

  • Being able to inject existing services used with Guice
  • Integration with Bean Validation
  • Serve resources asynchronously

Google search yielded the following:

  • Rapidoid - Simple REST framework with async support but no integration with Guice at the time
  • RESTEasy - Implementation of the JAX-RS spec. Has examples of usage with guice, writing async resources and Bean Validation support but no easy way to use with both async resources and Guice.
  • Jersey - Reference implementation of the JAX-RS spec, same as RESTEasy there was no easy way to answer both async and Guice requirements together.

After spending roughly 1.5 days fighting with those libraries and not getting what I wanted, I decided to go with Jersey as this is RI of well defined spec.

Why this module?

I could not find a library which binded the two together: Jersey and Guice. I tried the following:

Without any working solution, I sat down to write my own.

Contribution

  • Fork
  • Code
  • ./mvnw test
  • Issue a PR :)