LoginSignup
5
5

More than 5 years have passed since last update.

GlassFish4.0でJAX-RSのクラスにCDIでインジェクションしようとしたらUnsatisfiedDependencyExceptionが発生するバグ?

Posted at

英語ちゃんと読んでないからもしかしたら違うかも。
でも、一応動くようにはなった。

環境

OS

Windows7 64bit

GlassFish

4.0 open source edition

Java

1.7.0_40

発生した問題

JAX-RS のリソースクラスに CDI で POJO クラスをインジェクションしようとしたら、 UnsatisfiedDependencyException が発生してインジェクションできないという現象が発生した。

エラーになったりインジェクションできたりと、発生の条件がやや曖昧。

JAX-RSのリソースクラス
import javax.inject.Inject;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;

@Path("hoge")
public class HogeResource {

    @Inject
    private FugaService service;

    @GET
    @Path("{id}/definition")
    public String requireDefinition(@PathParam("id") long id) {
        return this.service.execute(id);
    }
}
スタックトレース(一部)
org.glassfish.hk2.api.UnsatisfiedDependencyException: There was no object available for injection at Injectee(略)
    at org.jvnet.hk2.internal.ThreeThirtyResolver.resolve(ThreeThirtyResolver.java:74)
    at org.jvnet.hk2.internal.ClazzCreator.resolve(ClazzCreator.java:191)
    at org.jvnet.hk2.internal.ClazzCreator.resolveAllDependencies(ClazzCreator.java:214)
    at org.jvnet.hk2.internal.ClazzCreator.create(ClazzCreator.java:311)
    at org.jvnet.hk2.internal.SystemDescriptor.create(SystemDescriptor.java:448)
    at org.glassfish.jersey.process.internal.RequestScope.findOrCreate(RequestScope.java:157)
    at org.jvnet.hk2.internal.Utilities.createService(Utilities.java:2204)
    at org.jvnet.hk2.internal.ServiceLocatorImpl.getService(ServiceLocatorImpl.java:579)
    at org.jvnet.hk2.internal.ServiceLocatorImpl.getService(ServiceLocatorImpl.java:566)
(後略)

同じ問題っぽい不具合報告

[#GLASSFISH-20597] UnsatisfiedDependencyException is thrown by JAX-RS, Bean Validation and CDI integration - Java.net JIRA
↑で同じっぽい問題に遭遇している人が不具合報告していた。

コメントに原因と対処法が書いてあり、どうも Jersey のバグらしく、 GlassFish 4.0.1 でこのバグ修正が反映されるらしい。
4.0.1 が出るまでは Jersey のバグフィックス版 jar を GlassFish 4.0 に入っている jar と差し替える必要があるらしい。

The fix will be present in GF 4.0.1 and after the official release of GF 4.0 we will provide an IPS update package with Jersey where this issue is fixed. (I'll let you know when the package is available)

In the meantime you can download the jersey-gf-cdi from maven central(http://repo1.maven.org/maven2/org/glassfish/jersey/containers/glassfish/jersey-gf-cdi/2.0/jersey-gf-cdi-2.0.jar), replace this module in glassfish4/glassfish/modules and delete the osgi cache glassfish4/glassfish/domains/domain1/osgi-cache/felix. This should fix the issue.

対処方法

2013/10/23 現在、 4.0.1 はリリースされてないっぽいので、説明されている方法で応急処置をする。

バグ修正版の jar を入手する。

ここ(http://repo1.maven.org/maven2/org/glassfish/jersey/containers/glassfish/jersey-gf-cdi/2.0/jersey-gf-cdi-2.0.jar) から jar をダウンロードする。

既存の jar と置き換える

<GlassFishインストールフォルダ>\glassfish\modules\jersey-gf-cdi.jar を取り除いて、 jersey-gf-cdi-2.0.jar を配置する。

キャッシュファイルを削除する

<GlassFishインストールフォルダ>\glassfish\domains\\<ドメインフォルダ>\osgi-cache\felix フォルダを削除する。

これで GlassFish を再起動したら、エラーが発生しなくなった。

参考

5
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
5