users@glassfish.java.net

Re: JAX-RS and CDI integration using Glassfish v3

From: Paul Sandoz <Paul.Sandoz_at_Sun.COM>
Date: Thu, 21 Jan 2010 15:38:22 +0000

On Jan 20, 2010, at 10:01 PM, glassfish_at_javadesktop.org wrote:

> In order to deploy a JAX-RS endpoint (ie: @javax.ws.rs.Path), I seem
> to have to configure the Jersey servlet in web.xml
> (com.sun.jersey.spi.container.servlet.ServletContainer, and
> parameter, com.sun.jersey.config.property.packages). This works
> well, but is there a more portable JEE6 way?
>
> Another annoying side-effect of having to do this is that the Jersey
> servlet bootstrapper does not perform CDI injections on the endpoint.
>
> However, if I mark the endpoint with @javax.annotation.ManagedBean,
> then CDI injections work fine. However, shouldn't @javax.ws.rs.Path
> be enough to enable CDI injections? Am I missing something in the
> JEE6 spec?
>

There are two ways CDI managed beans are enabled:

1) instantiated by CDI, life-cycle managed by Jersey. Annotate with
@ManagedBean and optionally annotate with a Jersey
      scope annotation.

2) instantiated and managed by CDI. Annotate with a CDI scope
annotation, like @RequestScoped (no @ManagedBean is
      required)


The reason for requiring an annotation is CDI can change the contract.
For example section 3.7.1 of the 299 spec states:

If a bean class does not explicitly declare a constructor using
@Inject, the constructor that accepts no parameters is the
bean constructor.

and section 5.1.1 of the 299 spec states:

5.5.1. Injection using the bean constructor
When the container instantiates a managed bean or session bean with a
constructor annotated @Inject, the container calls
this constructor, passing an injectable reference to each parameter.
If there is no constructor annotated @Inject, the con-
tainer calls the constructor with no parameters.


So if you have a root resource class like the following:

   @Path("foo")
   public class MyResource {
     public MyResource(@QueryParam("x") String x) { ... }
   }

then that class would fail if CDI was enabled by default. Other cases
are related to client proxies.

Note that the JAX-RS specification does not require that JAX-RS based
artifacts be bound such that 330's @Inject can be utilized. Jersey's
integration does not currently support it, so the following will not
work:

   @Path("foo")
   public class MyResource {
     @Inject
     public MyResource(@QueryParam("x") String x) { ... }
   }

it is something we need to implement (and may be non-trivial).

Paul.


> Thanks.
> [Message sent by forum member 'jeremynorris' (jnorris_at_pattern73.com)]
>
> http://forums.java.net/jive/thread.jspa?messageID=382037
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_glassfish.dev.java.net
> For additional commands, e-mail: users-help_at_glassfish.dev.java.net
>