jsr339-experts@jax-rs-spec.java.net

[jsr339-experts] SubResource Injection "Problem" Sample

From: Adam Bien <abien_at_adam-bien.com>
Date: Thu, 10 Mar 2011 11:12:08 +0100

Hello *,

The problem is already visible in the sample application "CustomerDB" shipped with NetBeans. New -> Sample -> Java Web Services -> REST Customer Database Java EE 5

@Path("/customers/")
public class CustomersResource {

    @Context
    protected ResourceContext resourceContext;


    @Path("{customerId}/")
    public CustomerResource getCustomerResource(@PathParam("customerId")
    Integer id) {
        CustomerResource resource = resourceContext.getResource(CustomerResource.class);
        resource.setId(id);
        return resource;
    }

}

public class CustomerResource {
    @Context
    protected UriInfo uriInfo;
    @Context
    protected ResourceContext resourceContext;
}


The id is passed to the CustomerResource and CustomerResource needs injection, so a Jersey-proprietary class "ResourceContext" has to be used for sub-resource creation. But: you cannot create EJB or managed beans this way.