0

I have used this setup successfully in previous methods with supervision so I'm probably doing something wrong now. I am mainly using Guice for its AOP part.

Relevant sections of pom.xml:

    <dependency>
        <groupId>io.dropwizard</groupId>
        <artifactId>dropwizard-core</artifactId>
        <version>0.7.0</version>
        <exclusions>
            <exclusion>
                <groupId>org.jboss.logging</groupId>
                <artifactId>jboss-logging</artifactId>
            </exclusion>
        </exclusions>
    </dependency>

    <dependency>
        <groupId>com.google.inject</groupId>
        <artifactId>guice</artifactId>
        <version>4.0</version>
    </dependency>

    <dependency>
        <groupId>aopalliance</groupId>
        <artifactId>aopalliance</artifactId>
        <version>1.0</version>
    </dependency>

    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <version>1.16.6</version>
        <scope>provided</scope>
    </dependency>

BaseBindingModule:

public class BaseBindingModule extends AbstractModule{

    @Override
    protected void configure() {
        bindInterceptor(any(), annotatedWith(SendCrashReport.class), new CrashReport());    
        bind(CabManager.class).to(CabManagerImpl.class);
    }
}

The file where resources are registered:

public class BucService extends Application<BucConfiguration> {
    public static void main(String[] args) throws Exception {
        new BucService().run(args);
    }

    @Override
    public void initialize(Bootstrap<BucConfiguration> bootstrap) {
        //bootstrap.setName("hello-world");
    }

    @Override
    public void run(BucConfiguration configuration,
                    Environment environment) {
        final String template = configuration.getTemplate();
        final String defaultName = configuration.getDefaultName();
        environment.jersey().register(new HelloWorldResource(template, defaultName));

        Injector injector = Guice.createInjector(new BaseBindingModule ());
        environment.jersey().register(injector.getInstance(CabResource.class)); // this does not work
      //  environment.jersey().register(new CabResource()); // this works
    }
}

The actual resource file:

@Path("/cabs")
@Produces(MediaType.APPLICATION_JSON)
public class CabResource {

    @Inject
    CabManager manager;


    public CabResource() {
    }

    @GET
    @Path("price")
    @Produces(MediaType.APPLICATION_JSON)
    public List<PriceEstimate> getCheapestCabs() {

        return manager.callB();
    }

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    @SendCrashReport
    public List<PriceEstimate> getNearestCabs(@Context UriInfo uriInfo) {

        return manager.callA();
      }

}

When I try to register the resource using Guice Injector.getInstance(), it doesn't get registered and I can't access any of the REST API's.

However if I simply register the resource as new CabResource(), the API's are visible. But in that case I wont be able to make GUICE AOP to work as per this answer: https://stackoverflow.com/a/8862599/1443801

What am I missing?

Thanks.

Edit: This is what shows when I run the jar file. Note that there are no cab API's.

INFO  [2016-04-01 13:26:14,785] io.dropwizard.server.ServerFactory: Starting BucService
INFO  [2016-04-01 13:26:14,876] org.eclipse.jetty.setuid.SetUIDListener: Opened application@758a34ce{HTTP/1.1}{0.0.0.0:9000}
INFO  [2016-04-01 13:26:14,876] org.eclipse.jetty.setuid.SetUIDListener: Opened admin@7ec3394b{HTTP/1.1}{0.0.0.0:9001}
INFO  [2016-04-01 13:26:14,877] org.eclipse.jetty.server.Server: jetty-9.0.z-SNAPSHOT
INFO  [2016-04-01 13:26:14,965] com.sun.jersey.server.impl.application.WebApplicationImpl: Initiating Jersey application, version 'Jersey: 1.18.1 02/19/2014 03:28 AM'
INFO  [2016-04-01 13:26:15,060] io.dropwizard.jersey.DropwizardResourceConfig: The following paths were found for the configured resources:

    GET     /hello-world (com.buc.resources.HelloWorldResource)

INFO  [2016-04-01 13:26:15,262] org.eclipse.jetty.server.handler.ContextHandler: Started i.d.j.MutableServletContextHandler@6c451c9c{/,null,AVAILABLE}
INFO  [2016-04-01 13:26:15,263] io.dropwizard.setup.AdminEnvironment: tasks = 

    POST    /tasks/gc (io.dropwizard.servlets.tasks.GarbageCollectionTask)

WARN  [2016-04-01 13:26:15,264] io.dropwizard.setup.AdminEnvironment: 
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!    THIS APPLICATION HAS NO HEALTHCHECKS. THIS MEANS YOU WILL NEVER KNOW      !
!     IF IT DIES IN PRODUCTION, WHICH MEANS YOU WILL NEVER KNOW IF YOU'RE      !
!    LETTING YOUR USERS DOWN. YOU SHOULD ADD A HEALTHCHECK FOR EACH OF YOUR    !
!         APPLICATION'S DEPENDENCIES WHICH FULLY (BUT LIGHTLY) TESTS IT.       !
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
INFO  [2016-04-01 13:26:15,269] org.eclipse.jetty.server.handler.ContextHandler: Started i.d.j.MutableServletContextHandler@3dddbe65{/,null,AVAILABLE}
INFO  [2016-04-01 13:26:15,279] org.eclipse.jetty.server.ServerConnector: Started application@758a34ce{HTTP/1.1}{0.0.0.0:9000}
INFO  [2016-04-01 13:26:15,280] org.eclipse.jetty.server.ServerConnector: Started admin@7ec3394b{HTTP/1.1}{0.0.0.0:9001}

Further clarification based on the comments:

I do get an instance using injector.getInstance(CabResource.class).

For example:

CabResource a = new CabResource();
CabResource b = injector.getInstance(CabResource.class);

a.getCheapestCabs(); 
b.getCheapestCabs();

both of the calls work.

Community
  • 1
  • 1
Paagalpan
  • 1,261
  • 2
  • 16
  • 25
  • Adding your error log would probably help some – J2B Apr 01 '16 at 13:19
  • There's no error. It just doesnt register. I can run the jar nicely. I just cant access the API's. It says 404 not found. – Paagalpan Apr 01 '16 at 13:24
  • I still added the output I get when running the jar. – Paagalpan Apr 01 '16 at 13:29
  • injector.getInstance(CabResource.class), does that even return anything? It would probably return nothing? Since it has not been registered in Guice? – J2B Apr 01 '16 at 13:29
  • I think I read it a bit too quick. Try binding the resource in your module – J2B Apr 01 '16 at 13:37
  • It definitely returns an instance. Because I can call injector.getInstance(CabResource.class).getCheapestCabs(...). Binding it to what? I wrote bind (CabResource.class); in the BaseBindingModule. Still no result. – Paagalpan Apr 01 '16 at 14:10
  • bind(CapResource.class).in(Singleton.class) in your module I would suggest. I agree with J2B, it seems odd to ask the injector for an instance, if you never bind one in the module first? There are frameworks that do that work automatically for you by the way: https://github.com/xvik/dropwizard-guicey – pandaadb Apr 01 '16 at 14:41
  • @pandaadb This doesnt seem to work either. Anyways, I was getting a correct instance using injector.getInstance(CabResource.class) as well because I was able to call its methods. – Paagalpan Apr 01 '16 at 14:56
  • @Paagalpan can you try upgrading DW to at least 0.8.4? I tried your example with and without interceptors. I have a suspicion that jersey may not be able to detect that your intercepted class is a resource since it is wrapping it into a proxy. In any case, I tried it with and without and with DW 0.8.4. I can not reproduce your error. Try debugging your code and see what type the Resource has too after it gets returned from guice injector. – pandaadb Apr 01 '16 at 15:24
  • @pandaadb Sorry for the late response. Updating dropwizard I get some weird errors. Maybe some cross dependency issues with some other project. Anyways, I'll try to resolve that if possible. Thanks – Paagalpan Apr 03 '16 at 10:57

1 Answers1

0

Have you considered using https://github.com/HubSpot/dropwizard-guice ?

<dependency>
  <groupId>com.hubspot.dropwizard</groupId>
  <artifactId>dropwizard-guice</artifactId>
  <version>${dropwizard.guice.version}</version>
</dependency>

Such as this guy did https://gitlab.com/michael-lloyd-lee/dropwizard-guice-example/tree/master

J2B
  • 1,703
  • 2
  • 16
  • 31
  • Will look into it. Thanks. Though under supervision at a different job, I had run the same setup successfully without the need of these libraries. So, I would really like to resolve the issue without using another external library and find out the issue with current code. – Paagalpan Apr 01 '16 at 13:25