Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use guice-servlet with Jersey 2.0?

Is there any sample code demonstrating how to use guice-servlet with Jersey 2.0?

like image 805
Gili Avatar asked Jun 24 '13 21:06

Gili


5 Answers

https://github.com/Squarespace/jersey2-guice seems to be the first genuine Guice integration for Jersey 2 but it requires version 2.11+.

NOTE: I haven't tested this, but the idea is sound.

like image 91
Gili Avatar answered Nov 15 '22 00:11

Gili


Yes, I've adapted an example and it's available here - https://github.com/piersy/jersey2-guice-example-with-test

I've updated the example code now, its got a test using jetty and another using tomcat.

like image 20
PiersyP Avatar answered Nov 14 '22 23:11

PiersyP


There is a page at HK2 official about correct guice implementation: https://javaee.github.io/hk2/guice-bridge.html

You should create your Injector something like this:

  public class GuiceConfig extends ResourceConfig {

        @Inject
        public GuiceConfig(ServiceLocator serviceLocator) {
            this();
            GuiceBridge.getGuiceBridge().initializeGuiceBridge(serviceLocator);
            GuiceIntoHK2Bridge guiceBridge = serviceLocator.getService(GuiceIntoHK2Bridge.class);
            guiceBridge.bridgeGuiceInjector(GuiceListener.createBiDirectionalGuiceBridge(serviceLocator));
        }

        public GuiceConfig() {
            packages(Injections.packages);
            addProperties(Injections.propertiesMap);
        }
    }

And code from the doc should be upgraded like:

   @WebListener
    public class GuiceListener extends GuiceServletContextListener {

        @Override
        public void contextInitialized(ServletContextEvent servletContextEvent) {
            Locale.setDefault(Locale.ENGLISH);
            super.contextInitialized(servletContextEvent);
        }

        public static volatile Injector injector = null;

        @Override
        protected Injector getInjector() {
            return injector;

        }

        @SuppressWarnings("unchecked")
        private static Module getModule() {
            return binder -> {
                Injections.singletonInterfaces.forEach((i, c) -> binder.bind(i).to(c).in(Scopes.SINGLETON));
                Injections.singletonClasses.forEach(c -> binder.bind(c).in(Scopes.SINGLETON));
            };
        }

        static synchronized Injector createBiDirectionalGuiceBridge(ServiceLocator serviceLocator) {

            return GuiceListener.injector = createBiDirectionalGuiceBridge(serviceLocator, getModule());
        }

    }

Using the maven dependency at your pom.xml

   <dependency>
        <groupId>org.glassfish.hk2</groupId>
        <artifactId>guice-bridge</artifactId>
        <version>2.3.0</version>
    </dependency>

https://github.com/phxql/jersey2-guice doesn't work with jersey 2.22 and guice 4.0.

like image 39
Globber Avatar answered Nov 15 '22 00:11

Globber


This is a minimum working PoC which wires Jersey 2 and Guice together:

https://github.com/phxql/jersey2-guice

like image 1
phXql Avatar answered Nov 14 '22 23:11

phXql


I've already done in this sample:

https://github.com/jbescos/tododev

You have to register the class https://github.com/jbescos/tododev/blob/master/jersey2-guice/src/main/java/es/tododev/rest/ApplyGuiceContextFilter.java in your ResourceConfig, and the guice injector binded in an AbstractModule.

@Provider
@PreMatching
public class ApplyGuiceContextFilter implements ContainerRequestFilter, ContainerResponseFilter {

    @Inject
    public ApplyGuiceContextFilter(ServiceLocator serviceLocator, Injector injector) {
        GuiceBridge.getGuiceBridge().initializeGuiceBridge(serviceLocator);

        GuiceIntoHK2Bridge guiceBridge = serviceLocator.getService(GuiceIntoHK2Bridge.class);
        guiceBridge.bridgeGuiceInjector(injector);
    }

    @Override
    public void filter(final ContainerRequestContext requestContext) throws IOException {

    }

    @Override
    public void filter(final ContainerRequestContext requestContext, final ContainerResponseContext responseContext) throws IOException {}
}  

This is the ResouceConfig:

public class RestConfig extends ResourceConfig {

    @Inject
    public RestConfig() {
        this(Guice.createInjector(new Module(){
            @Override
            public void configure(Binder arg0) {
                // TODO Auto-generated method stub
            }
        }));
    }

    // Test
    public RestConfig(Injector injector) {
        packages(ResourceSample.class.getPackage().getName());
        register(ApplyGuiceContextFilter.class);
        register(new LoggingFilter(Logger.getLogger(LoggingFilter.class.getName()), true));

        property(ServerProperties.TRACING, "ALL");
        register(new RestBinder(injector));
    }

    private static class RestBinder extends AbstractBinder{

        private final Injector injector;

        private RestBinder(Injector injector){
            this.injector = injector;
        }

        @Override
        protected void configure() {
            bind(injector).to(Injector.class);
        }

    }

}
like image 1
ravenskater Avatar answered Nov 15 '22 00:11

ravenskater