Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

Guice module for starting Jetty based rest server with Jersey

License

NotificationsYou must be signed in to change notification settings

logzio/guice-jersey

Repository files navigation

Build StatusCoverage StatusMaven Central

⚠️ Important Notice

As of September 19, 2024, we have decided to no longer maintain this open-source project. Please feel free to fork the repository and continue its development or publish it independently. We appreciate your support and contributions thus far!

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. AddJerseyModule to your Guice Injector
  2. Configure packages to scan for resources and a port to expose
  3. Get instance ofJerseyServer and start consuming your Restful resources
publicclassMain {publicstaticvoidmain(String[]args)throwsException {JerseyConfigurationconfiguration =JerseyConfiguration.builder()            .addPackage("com.example.resources")            .addPort(8080)            .build();List<Module>modules =newArrayList<>();modules.add(newJerseyModule(configuration));modules.add(newAbstractModule() {@Overrideprotectedvoidconfigure() {// 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 supportbut 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 :)

[8]ページ先頭

©2009-2025 Movatter.jp