guice Using Guice with Jersey 2 (JAX-RS RI) Getting Guice injections on JAX-RS resources

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Extensions
> Step 2: And Like the video. BONUS: You can also share it!

Example

You will need the guice-bridge in your project.

@ApplicationPath("api")
public class ApiRest extends ResourceConfig {  
    @Inject
    public ApiRest(ServiceLocator serviceLocator, ServletContext servletContext) {
        packages("net.sargue.app.api");

        GuiceBridge.getGuiceBridge().initializeGuiceBridge(serviceLocator);
        GuiceIntoHK2Bridge guiceBridge = serviceLocator.getService(GuiceIntoHK2Bridge.class);
        Injector injector = (Injector) servletContext.getAttribute(Injector.class.getName());
        if (injector == null)
            throw new RuntimeException("Guice Injector not found");
        guiceBridge.bridgeGuiceInjector(injector);

        register(RolesAllowedDynamicFeature.class);
    }
}


Got any guice Question?