I created a project to test the dependency injection offered by Google Guice in my Jax-rs resources, using Resteasy.
My intentions are:
- Use multiple
@ApplicationPath
for the versions of my API. In each class annotated with@ApplicationPath
I load a set of classes for the specific version. - Each resource have a
@Inject
(from Google Guice) in his constructor to inject some services.
I created two classes annotated with @ApplicationPath
: ApplicationV1RS
and ApplicationV2RS
. In both I added the same resources classes (UserResource
and HelloResource
), only for my test.
My Module is configured like this:
public class HelloModule implements Module
{
public void configure(final Binder binder)
{
binder.bind(IGreeterService.class).to(GreeterService.class);
binder.bind(IUserService.class).to(UserService.class);
}
}
When I call http://localhost:9095/v1/hello/world
or http://localhost:9095/v2/hello/world
, I receive the same error:
java.lang.RuntimeException: RESTEASY003190: Could not find constructor
for class: org.jboss.resteasy.examples.guice.hello.HelloResource
Well, as I expected, this not works. The Google Guice is not "smart" to instantiate the resource classes using the construtor for me.
But I can't find a way to work. To be really honest, I'm really confuse about how the Google Guice, Jetty and Resteasy play with each other in this scenario.
If I abandon the idea of use @ApplicationPath
, my resources work with Google Guice configuring my HelloModule
like this:
public class HelloModule implements Module
{
public void configure(final Binder binder)
{
binder.bind(HelloResource.class);
binder.bind(IGreeterService.class).to(GreeterService.class);
binder.bind(UserResource.class);
binder.bind(IUserService.class).to(UserService.class);
}
}
But in this case, I'm passing the control to register my resources (HelloResource
and UserResource
) to Guice. It's not flexible for me, I can't setup my multiple @ApplicationPath
.
So, what I'm missing or not understanding?
I created a project with the problemetic code. Is very easy to setup and test: https://github.com/dherik/resteasy-guice-hello/tree/so-question/README.md
Thanks!