Giter Club home page Giter Club logo

jerseyguice's Introduction

REST with Jersey2 and Guice

Bridge Jersey's HK2 to Guice Dependency Injection Framework

This project implements a RESTful Webservice using Jersey and Guice.

Jersey is a very popular Java toolkit that abstracts away the low level details of the client-server communication, while Guice is one of the most popular dependency injection frameworks.

Jersey uses its own depencency injection framework HK2. There is a bridge between Jersey and Guice, but it is not easy to elegantly configure REST endpoints that use Guice dependency injection. This is where this project comes in. It defines a RestServletModule, that allows you to configure your REST application in the same way as all the other parts within it. This RestServletModule adds a rest() and a packages() function to configure Jersey2. This pays off by giving us a clear and elegant application structure. A typical configuration would be:

public class MyServletContextListener extends GuiceServletContextListener {
    @Override
    protected Injector getInjector() {
        return Guice.createInjector(new RestServletModule() {
            @Override
            protected void configureServlets() {
                bind(WelcomeTexter.class);
                bind(HelloWorldServlet.class).in(Scopes.SINGLETON);
                serve("/hello").with(HelloWorldServlet.class);
                
                rest("/app/*").packages("com.example.jerseyguice.app");
                
                bind(User.class);
                bind(UserDao.class).to(InMemoryUserDao.class);
                bind(InMemoryUserDatabase.class);
            }
        });
    }
}

The solution is made for the google app engine but it should work on any application server.

For details see my blog about RESTful Webservices with Jersey 2 and Guice. For more information about the design principles behind it see my other blogs.

The source code for the tutorials can be found in the tutorials subfolder.

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.