Fork me on GitHub

Guice Integration

NOTE: The Guice Integration does not have dependencies on other parts of Soabase and, so, can be used independently if needed.

The Guice Integration includes:

  • GuiceBundle

    The GuiceBundle integrates the Guice/HK2 Bridge so that objects bound via Guice are available to your Jersey 2.0 resources.

  • JerseyGuiceModule

    The JerseyGuiceModule adds most of the functionality of Guice's ServletModule and JerseyServletModule. All registrations are forwarded to the appropriate Dropwizard/Jersey/Jetty methods.

Features

The following features from Guice's ServletModule and the JerseyServletModule are supported/emulated:

  • filter() for specifying Servlet Filters
  • serve() for specifying Servlets
  • All Guice bindings in the specified Injector are available in your Jersey 2.0 resources
  • @RequestScoped and @SessionScoped
  • Injected Jersey Providers
  • Injected Jersey root resources
  • Injected Jersey components:
    • ContainerRequestFilter
    • ContainerResponseFilter
    • ClientResponseFilter
    • ClientRequestFilter
    • DynamicFeature
    • ReaderInterceptor
    • WriterInterceptor
  • Request-scoped bindings for...
    • HttpServletRequest
    • HttpServletResponse
    • ServletContext
    • HttpSession
    • Request parameter map as Map<String, String[]>
    • ContainerRequestContext
    • Request
    • UriInfo
    • HttpHeaders
    • MessageBodyWorkers
    • SecurityContext
    • ExtendedResourceContext
    • ResourceContext
  • Additional features:
    • JerseyGuiceModule.configurable() - Allows Jersey 2.0 style registrations

Artifact

The Guice Integration is available from Maven Central. Use any dependency tool (e.g. Maven or Gradle) to add the artifact to your application's project:

io.soabase:soabase-guice

Usage

Add the GuiceBundle to your application. Its constructor takes an InjectorProvider that returns the Guice Injector to use. Any objects that are part of the Injector will be available to your Jersey resources. Additionally, any bound Jersey Providers will be registered via Dropwizard.

InjectorProvider injectorProvider = ...
bootstrap.addBundle(new GuiceBundle<>(injectorProvider));

The JerseyGuiceModule is used exactly as Guice's ServletModule and JerseyServletModule. Add the module to your injector and override the configureServlets() method. Guice's ServletModule documentation applies to JerseyGuiceModule. Additionally, any objects registered via SoaFeatures and the SoaFeatures instance itself are bound into the Guice injector.

Example

Example code for using the Guice integration is here: Soabase Guice Example