lyonssp
lyonssp

Reputation: 155

Dropwizard: dropwizard-guicey sub-resource locators

I'm migrating my application from Jersey2/HK2 to Dropwizard. I'm using dropwizard-guicey to integrate Guice into my application while maintaining some of my HK2 bindings/features.

Dropwizard-guicey binds all resources in singleton scope by default, which leaves me wondering -- what is the suggested way to bind sub-resources in this situation? In Jersey, I can do something as simple as

@Path("{pathParamId}/sub-resource")
public Class<MySubResource> mySubResource() {
    return MySubResource.class;
}

But my subresource class then looks like this

public class MySubResource {
    private String id;

    public MySubResource(@PathParam("pathParamId") id) {
        this.id = id;
    }
}

It doesn't seem that Dropwizard is able to pick up on the dependencies that Guice binds in MySubResource, since the resources are all managed by Jersey.

What I'm ultimately looking for is the per-request bindings that Jersey gives by default for resources and Guice to do the injection beneath that level.

Upvotes: 0

Views: 179

Answers (1)

xvik
xvik

Reputation: 366

In your example, root resource will be managed by guice (installed by resource installer) and sub resource will be created by HK (per request).

To use guice services in sub resource you need to enable hk guice bridge:

  • Add org.glassfish.hk2:guice-bridge:2.5.0-b32 dependency
  • enable bridge option with .option(GuiceyOptions.UseHkBridge, true).

I add complete demo to the samples repo. It also shows how your case could be implemented with pure guice.

Upvotes: 2

Related Questions