JAX-RS 2.0 と管理 Bean の統合
WebSphere® Application Server traditional の Java™ API for RESTful Web Services (JAX-RS) 2.0 は、ルート・リソース・クラス、プロバイダー、およびアプリケーション・サブクラスとしての管理 Bean の使用をサポートします。
- 管理 Bean を JAX-RS リソース、プロバイダー、またはアプリケーションとして使用するには、@ManagedBean を使用してこれらのクラスにアノテーションを付けます。例えば、以下のように、インターセプター管理 Bean フィーチャーを使用します。
@ManagedBean ("JaxrsManagedBean" ) @Path ("/managedbean" ) public class ManagedBeanResource { public static class MyInterceptor { @AroundInvoke public Object around(InvocationContext ctx) throws Exception { System. out .println("around() called" ); return ctx.proceed(); } } @GET @Produces("text/plain") @Interceptors(MyInterceptor. class ) public String getIt() { return "Hi managed bean!" ; } }
管理 Bean とともに JAX-RS 2.0 を使用する場合の制約事項
リソース注入は、Contexts and Dependency Injection (CDI) で管理されている、以下の JAX-RS コンポーネント・クラスによってのみサポートされます。
- アプリケーション・サブクラス
- プロバイダー
- ルート・リソース・クラス
具体的には、管理 Bean インスタンスを特定の JAX-RS コンポーネント・クラスに注入するには、このコンポーネント・クラスを CDI Bean として認識および管理できるようにする必要があります。
例えば、以下のように printMyName 管理 Bean インターフェースを JAX-RS ルート・リソース・クラスに注入するには、空の beans.xml ファイルを .WAR file/WEB-INF リポジトリーに追加する必要があります。
@Path ("/managedbean" )
public class ManagedBeanResource {
@Resource(name = "printMyName" )
private PrintMyName printMyName ;
@GET
@Produces("text/plain")
public String getIt() {
printMyName .print();
return "Hi managed bean!" ;
}
}
@ManagedBean ("printmyname" )
public class PrintMyName {
public void print() {
// TODO Auto-generated method stub
System. out .println("Injection of ManagedBean is successful" );
}
}