1. ホーム
  2. java

[解決済み] Jersey 2.0による依存性注入法

2022-10-18 09:40:11

質問

以前のJersey 1.xの知識なしでゼロから始めて、私のJersey 2.0プロジェクトで依存性注入をセットアップする方法を理解するのに苦労しています。

HK2 が Jersey 2.0 で利用可能であることも理解していますが、Jersey 2.0 統合に役立つドキュメントを見つけることができないようです。

@ManagedBean
@Path("myresource")
public class MyResource {

    @Inject
    MyService myService;

    /**
     * Method handling HTTP GET requests. The returned object will be sent
     * to the client as "text/plain" media type.
     *
     * @return String that will be returned as a text/plain response.
     */
    @GET
    @Produces(MediaType.APPLICATION_JSON)
    @Path("/getit")
    public String getIt() {
        return "Got it {" + myService + "}";
    }
}

@Resource
@ManagedBean
public class MyService {
    void serviceCall() {
        System.out.print("Service calls");
    }
}

pom.xml

<properties>
    <jersey.version>2.0-rc1</jersey.version>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.glassfish.jersey</groupId>
            <artifactId>jersey-bom</artifactId>
            <version>${jersey.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

<dependencies>
    <dependency>
        <groupId>org.glassfish.jersey.core</groupId>
        <artifactId>jersey-common</artifactId>
    </dependency>
    <dependency>
        <groupId>org.glassfish.jersey.core</groupId>
        <artifactId>jersey-server</artifactId>
    </dependency>
    <dependency>
        <groupId>org.glassfish.jersey</groupId>
        <artifactId>jax-rs-ri</artifactId>
    </dependency>
</dependencies>

コンテナを起動してリソースを提供することはできますが、MyService に @Inject を追加するとすぐに、フレームワークが例外をスローします。

SEVERE: Servlet.service() for servlet [com.noip.MyApplication] in context with path [/jaxrs] threw exception [A MultiException has 3 exceptions.  They are:
1. org.glassfish.hk2.api.UnsatisfiedDependencyException: There was no object available for injection at Injectee(requiredType=MyService,parent=MyResource,qualifiers={}),position=-1,optional=false,self=false,unqualified=null,1039471128)
2. java.lang.IllegalArgumentException: While attempting to resolve the dependencies of com.noip.MyResource errors were found
3. java.lang.IllegalStateException: Unable to perform operation: resolve on com.noip.MyResource
] with root cause
org.glassfish.hk2.api.UnsatisfiedDependencyException: There was no object available for injection at Injectee(requiredType=MyService,parent=MyResource,qualifiers={}),position=-1,optional=false,self=false,unqualified=null,1039471128)
    at org.jvnet.hk2.internal.ThreeThirtyResolver.resolve(ThreeThirtyResolver.java:74)



私のスタータープロジェクトはGitHubで公開されています。 https://github.com/donaldjarmstrong/jaxrs

どのように解決するのですか?

を定義する必要があります。 AbstractBinder を定義し、JAX-RS アプリケーションに登録する必要があります。バインダーは、依存性注入がどのようにクラスを作成すべきかを指定します。

public class MyApplicationBinder extends AbstractBinder {
    @Override
    protected void configure() {
        bind(MyService.class).to(MyService.class);
    }
}

いつ @Inject 型のパラメータやフィールドで検出された場合 MyService.class . このバインダーを使用するには、JAX-RS アプリケーションに登録する必要があります。あなたの MyService で、JAX-RSアプリケーションを以下のように定義します。

web.xml

を実装します。 <servlet> <servlet-name>MyApplication</servlet-name> <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class> <init-param> <param-name>javax.ws.rs.Application</param-name> <param-value>com.mypackage.MyApplication</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>MyApplication</servlet-name> <url-pattern>/*</url-pattern> </servlet-mapping> クラス (上記で指定した MyApplication ).

init-param

依存性注入を指定するバインダーをクラスのコンストラクタに登録し、アプリケーションにRESTリソースの場所を伝える(あなたの場合。 public class MyApplication extends ResourceConfig { public MyApplication() { register(new MyApplicationBinder()); packages(true, "com.mypackage.rest"); } } を使用します)。 MyResource メソッド呼び出しを使用します。