ef-labs / vertx-guice

Create Vert.x Modules and Verticles with dependency injection using Guice
MIT License
59 stars 24 forks source link

How to create a main class to start the service with Guice and Jackson #17

Closed RaviH closed 6 years ago

RaviH commented 6 years ago

Hi,

I am trying to figure out how to start the app/service via a main class.

Below is what I have and it's not working. What am I missing?

public class VertxExmbedded {

    public static void main(String[] args) {

        Vertx vertx = Vertx.vertx();

        vertx.runOnContext(aVoid -> {

            // Set up the jersey configuration
            // The minimum config required is a package to inspect for JAX-RS endpoints
            vertx.getOrCreateContext().config()
                    .put("jersey", new JsonObject()
                            .put("port", 8080)
                            .put("packages", new JsonArray()
                                    .add(TitleRS.class.getPackage().getName())));

            // Use a service locator (HK2 or Guice are supported by default) to create the jersey server
            final ServiceLocator serviceLocator = ServiceLocatorUtilities.createAndPopulateServiceLocator("default");
            GuiceBridge.getGuiceBridge().initializeGuiceBridge(serviceLocator);
            final VertxExmbedded vertxExmbedded = new VertxExmbedded();
            final Injector injector = Guice.createInjector(new CustomBinder().foo());
            vertxExmbedded.tieInjectorToLocator(serviceLocator, injector);
//            ServiceLocator locator = ServiceLocatorUtilities.bind(serviceLocator, new CustomBinder());
            JerseyServer server = serviceLocator.getService(JerseyServer.class);

            // Start the server which simply returns "Hello World!" to each GET request.
            server.start();

        });

    }
    public void tieInjectorToLocator(ServiceLocator aServiceLocator, Injector guiceInjector) {
        GuiceIntoHK2Bridge guiceBridge = aServiceLocator.getService(GuiceIntoHK2Bridge.class);
        guiceBridge.bridgeGuiceInjector(guiceInjector);
    }

}
RaviH commented 6 years ago

Figured it out. Posting here for posterity:

public class VertxExmbedded {

    public static void main(String[] args) {

        Vertx vertx = Vertx.vertx();

        vertx.runOnContext(aVoid -> {

            // Set up the jersey configuration
            // The minimum config required is a package to inspect for JAX-RS endpoints
            vertx.getOrCreateContext().config()
                    .put("jersey", new JsonObject()
                            .put("port", 8080)
                            .put("base_path", "/videocatalogmiddle")
                            .put("resources", new JsonArray().add("com.charter.aesd.videocatalog")));

            final Injector injector = Guice.createInjector(new CustomBinder(vertx));
            final GuiceJerseyServer guiceJerseyServer = injector.getInstance(GuiceJerseyServer.class);
            guiceJerseyServer.start();
        });
    }
}