Coder Social home page Coder Social logo

guice-jersey's Introduction

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.13'

Maven

<dependency>
  <groupId>io.logz</groupId>
  <artifactId>guice-jersey</artifactId>
  <version>1.0.13</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 :)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.