ef-labs / vertx-jersey

Run jersey in vert.x
MIT License
150 stars 47 forks source link

Add filter to vertx.getOrCreateContext()? #70

Closed plutext closed 6 years ago

plutext commented 6 years ago

This is how I add https://github.com/ef-labs/vertx-jersey/blob/develop/examples/filter/src/main/java/com/englishtown/vertx/jersey/examples/FilterBinder.java to my setup. Maybe it helps someone (it took me a while to figure out).

   public static void main(String[] args) {

        VertxOptions vertxOptions = new VertxOptions();
        Vertx vertx = Vertx.vertx(vertxOptions);

        JsonArray jsonArray = new JsonArray();
        jsonArray.add(MyEndpoint.class.getPackage().getName());

        JsonArray jsonArray2 = new JsonArray();
        jsonArray2.add(FilterBinder.class.getName());

        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", jsonArray)
                            .put("instances", jsonArray2));

            // Use a service locator (HK2 or Guice are supported by default) to create the jersey server
            ServiceLocator locator = ServiceLocatorUtilities.bind(
                    new HK2JerseyBinder(), 
                    new HK2VertxBinder(vertx));

            JerseyServer server = locator.getService(JerseyServer.class);
            server.start();

        });

    }