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.