JAX-RS 2.0 integration with managed beans

Java™ API for RESTful Web Services (JAX-RS) 2.0 in WebSphere® Application Server tradicional supports the use of managed beans as root resource classes, providers, and application subclasses.

  • To use a managed bean as a JAX-RS resource, provider, or application, use the @ManagedBean to annotate these classes.
    For example, use the Interceptors managed bean feature as follows:
    @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!" ;
        }
    }

Restrictions on JAX-RS 2.0 with managed beans

Resource injection is only supported by the following JAX-RS component classes that are managed by Contexts and Dependency Injection (CDI):
  • Application subclasses
  • Providers
  • Root resource classes

Specifically, to inject a managed bean instance into a certain JAX-RS component class, you must ensure that this component class can be recognized and managed as a CDI bean.

For example, to inject the printMyName managed bean instance into a JAX-RS root resource class as follows, you must add an empty beans.xml file in the .WAR file/WEB-INF repository:
@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" );
    }

}

Ícone que indica o tipo de tópico Tópico de Referência



Ícone de registro de data e hora Última atualização: July 9, 2016 7:52
http://www14.software.ibm.com/webapp/wsbroker/redirect?version=cord&product=was-nd-mp&topic=rwbs_jaxrs2
Nome do arquivo: rwbs_jaxrs2.0_managedbeans.html