users@jersey.java.net

Re: [Jersey] Explicit registration of Jersey servlet ?

From: Paul Sandoz <Paul.Sandoz_at_Sun.COM>
Date: Thu, 18 Feb 2010 09:17:07 +0100

On Feb 18, 2010, at 5:42 AM, Moises Lejter wrote:

> An unrelated question - but on the same example :-)
> I thought that one had to use @ManagedBean on a JAX-RS resource, in
> order for the @Inject annotation to work? Am I behind the times?

No, it is just confusing.

Note the CD-based annotation @RequestScoped. CDI is enabled and that
annotation informs the runtime an instance of the class ActorResource
will be instantiated and managed by CDI in the scope of the HTTP
request.

The use of @ManagedBean is slightly different and will work in CDI and
non-CDI enabled environments. An annotated class with @ManagedBean
means that Jersey will defer to the container to instantiate the class
but Jersey will manage the bean. It is basically a way of getting EE-
realted stuff injected.

The "managed bean" term is defined to unify the many forms of managed
bean that are available in EE.

Hth,
Paul.


>
> Moises
>
> On Wed, Feb 17, 2010 at 10:37 PM, Arun Gupta <Arun.Gupta_at_sun.com>
> wrote:
> I have a webapp with the following resource:
>
> @Path("/actor/{id}")
> @RequestScoped
> public class ActorResource {
> @Inject SakilaBean sakila;
>
> @GET
> @Produces("application/json")
> public Actor getActor(@PathParam("id") int id) {
> System.out.println("id: " + id);
> return sakila.findActorById(id);
> }
> }
>
> This is deployed as simplewebapp.war on GlassFish v3.
>
> http://localhost:8080/simplewebapp/actor/4
>
> gives a 404. However adding:
>
> <servlet>
> <servlet-name>Jersey Web Application</servlet-name>
>
> <servlet-
> class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-
> class>
> </servlet>
>
> <servlet-mapping>
> <servlet-name>Jersey Web Application</servlet-name>
> <url-pattern>/sakila/*</url-pattern>
> </servlet-mapping>
>
> to web.xml, then the resource is available at:
>
> http://localhost:8080/simplewebapp/sakila/actor/4.
>
> Why Jersey servlet need to be registered explicitly ?
>
> -Arun
> --
> Need Application Server ? Download from http://glassfish.org
> Blog: http://blogs.sun.com/arungupta
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>
>