I 创建了一个项目使用 Resteasy 在我的 Jax-rs 资源中测试 Google Guice 提供的依赖项注入。
我的意图是:
- 使用多个
@ApplicationPath
对于我的 API 的版本。在每个类中注释为@ApplicationPath
我为特定版本加载一组类。
- 每个资源都有一个
@Inject
(来自 Google Guice)在他的构造函数中注入一些服务。
我创建了两个带有注释的类@ApplicationPath
: ApplicationV1RS
and ApplicationV2RS
。在这两个中我都添加了相同的资源类(UserResource
and HelloResource
),仅用于我的测试。
我的模块配置如下:
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);
}
}
当我打电话时http://localhost:9095/v1/hello/world
or http://localhost:9095/v2/hello/world
,我收到同样的错误:
java.lang.RuntimeException: RESTEASY003190: Could not find constructor
for class: org.jboss.resteasy.examples.guice.hello.HelloResource
嗯,正如我所料,这不起作用。 Google Guice 并不“聪明”地使用构造函数实例化资源类。
但我找不到工作的方法。说实话,我真的很困惑 Google Guice、Jetty 和 Resteasy 在这种情况下如何相互配合。
如果我放弃使用的想法@ApplicationPath
,我的资源与 Google Guice 一起配置我的HelloModule
像这样:
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);
}
}
但在这种情况下,我将传递控制权来注册我的资源(HelloResource
and UserResource
)到吉斯。这对我来说不灵活,我无法设置我的多个@ApplicationPath
.
那么,我缺少或不理解什么?
我使用有问题的代码创建了一个项目。设置和测试非常容易:https://github.com/dherik/resteasy-guice-hello/tree/so-question/README.md
Thanks!