Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use jersey 2.0 guice on grizzly

I want to use Guice + Jersey 2.0 on Grizzly. According to this How to use guice-servlet with Jersey 2.0? discussion there is no direct Guice integration for Jersey2 at present but it can be achieved using HK2 as a bridge. I also checked the sample project in Github https://github.com/piersy/jersey2-guice-example-with-test . This project is implemented using Jetty.

But my problem is to implement it in Grizzly. On Jetty it is used like this

  @Inject
public MyApplication(ServiceLocator serviceLocator) {
    // Set package to look for resources in
    packages("example.jersey");

    System.out.println("Registering injectables...");

    GuiceBridge.getGuiceBridge().initializeGuiceBridge(serviceLocator);

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

}

My problem on grizzly is , how to get this serviceLocator object?

Thank you.

like image 703
kasto_manche Avatar asked Aug 06 '13 12:08

kasto_manche


2 Answers

I have created the sample here https://github.com/oleksiys/samples/tree/master/jersey2-guice-example-with-test

The Grizzly initialization code looks like this:

final URI uri = UriBuilder.fromUri("http://127.0.0.1/")
        .port(8080).build();

// Create HttpServer
final HttpServer serverLocal = GrizzlyHttpServerFactory.createHttpServer(uri, false);

// Create Web application context
final WebappContext context = new WebappContext("Guice Webapp sample", "");

context.addListener(example.jersey.Main.class);

// Initialize and register Jersey ServletContainer
final ServletRegistration servletRegistration =
        context.addServlet("ServletContainer", ServletContainer.class);
servletRegistration.addMapping("/*");
servletRegistration.setInitParameter("javax.ws.rs.Application",
        "example.jersey.MyApplication");

// Initialize and register GuiceFilter
final FilterRegistration registration =
        context.addFilter("GuiceFilter", GuiceFilter.class);
registration.addMappingForUrlPatterns(EnumSet.allOf(DispatcherType.class), "/*");

context.deploy(serverLocal);

serverLocal.start();
like image 148
alexey Avatar answered Dec 04 '22 17:12

alexey


add dependecy

compile group: "org.glassfish.hk2", name: "guice-bridge", version: "2.4.0"

create feature

public class GuiceFeature implements Feature {

    @Override
    public boolean configure(FeatureContext context) {
        ServiceLocator serviceLocator = ServiceLocatorProvider.getServiceLocator(context);

        GuiceBridge.getGuiceBridge().initializeGuiceBridge(serviceLocator);
        GuiceIntoHK2Bridge guiceBridge = serviceLocator.getService(GuiceIntoHK2Bridge.class);

        Injector injector = Guice.createInjector(new AbstractModule() {
            @Override
            protected void configure() {
                bind(YYY.class).to(ZZZ.class);
            }
        });
        guiceBridge.bridgeGuiceInjector(injector);

        return true;
    }
}

register feature

ResourceConfig resourceConfig = new ResourceConfig();
resourceConfig.register(GuiceFeature.class);
like image 34
rrader Avatar answered Dec 04 '22 18:12

rrader