I created a project Resteasyを使用して、Jax-rsのリソースでGoogle Guiceが提供する依存性注入をテストします。ResteasyとGoogle Guice:@Injectionで複数の@ApplicationPathとリソースを使用する方法は?
私の意図は、次のとおりです。私のAPIのバージョンの
- 使用し、複数の
@ApplicationPath
。@ApplicationPath
で注釈が付けられた各クラスでは、特定のバージョンのクラスのセットを読み込みます。 - 各リソースには、サービスを注入するコンストラクタ内に
@Inject
(Google Guiceから)があります。
@ApplicationPath
:ApplicationV1RS
とApplicationV2RS
という2つのクラスを作成しました。両方とも私は同じリソースクラス(UserResource
とHelloResource
)を私のテストのためだけに追加しました。
私のモジュールは、次のように設定されている:私はhttp://localhost:9095/v1/hello/world
またはhttp://localhost:9095/v2/hello/world
呼び出すと
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);
}
}
、私は同じエラーが表示されます。
まあjava.lang.RuntimeException: RESTEASY003190: Could not find constructor
for class: org.jboss.resteasy.examples.guice.hello.HelloResource
、私は予想通り、これはない作品。 Google Guiceは、私のためにconstrutorを使ってリソースクラスをインスタンス化する "スマート"ではありません。
しかし、私は働く方法を見つけることができません。本当に正直なところ、このシナリオでGoogle Guice、Jetty、Resteasyがどう対戦するかは本当に混乱しています。
私が使用@ApplicationPath
の考えを放棄した場合、私のリソースは、このように私のHelloModule
を設定するGoogleのGuiceのでは動作:
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
とUserResource
)をGuiceに送信します。それは私のために柔軟ではない、私は複数の@ApplicationPath
を設定することはできません。
だから私は迷っているか、理解していないのですか?
問題のあるコードでプロジェクトを作成しました。セットアップとテストが非常に簡単です:https://github.com/dherik/resteasy-guice-hello/tree/so-question/README.md
ありがとう!
web.xmlはまだ必要ありませんか?私は正しい? – SayusiAndo