users@jax-rs-spec.java.net

[jax-rs-spec users] [jsr339-experts] Re: Re: Re: Re: Re: FYI: ResourceContext API proposed

From: Bill Burke <bburke_at_redhat.com>
Date: Fri, 31 Aug 2012 11:21:15 -0400

On 8/31/2012 9:42 AM, Marek Potociar wrote:
>> I'm against this feature. It appears, from the JIRA issue Marek linked
>> to, it was mainly about supporting the injection into EJB
>> subresources. It's not a JAX-RS concern - I agree with Bill.
>
> I finally found another MAJOR issue that has been closed as a duplicate
> of JAX_RS_SPEC-72:
> http://java.net/jira/browse/JAX_RS_SPEC-35
>
> Note the larger scope of the original issue. Also kindly check our older
> discussions on this:
> http://java.net/projects/jax-rs-spec/lists/jsr339-experts/archive/2012-04/message/17
>

Yup, my views now are pretty much consistent to my comments back then.
I wasn't sure then, but we now know Java EE has a manual injection API
(CDI BeanManager). So does Guice (factory), so does Spring. CDI and
EJB should support JAX-RS field injections. If you want to write a pure
standalone service, then pass information to constructor or method
parameters of the subresource instance.

Not sure about Spring, but with CDI you can even define injected beans
as request scoped, so the code would be really clean in a CDI environment:

@Path("/authors)
public class AuthorResource {

    @Inject AuthoredBookResource bookResource;

    @Path("{author}/books")
    public BookResource getBooks(@PathParam("author") String author) {
       bookResource.setAuthor(author);
       return bookResource;
    }
}


@Path("/books")
@RequestScoped
public class AuthoredBookResource extends BookResource {

    @Context UriInfo uriInfo;

    private String author;

    public void setAuthor(...) {...}

    @GET
    @Produces("application/json")
    public List<Books> getBooks() {

    }

}

If you have a forwarding mechanism, you could write this as:

@Path("/authors)
public class AuthorResource {

    @Context RequestForwardingAPI forward;

    @Path("{author}/books")
    @GET
    public Response getBooks(@PathParam("author") String author) {
       return forward.forwardTo("/books?author=" + author);
    }
}

1st case is you have class that is only a sub-source. 2nd case is that
you have a subresource that is also a root resource. What the exact
forwarding API is, I'm not sure yet.

-- 
Bill Burke
JBoss, a division of Red Hat
http://bill.burkecentral.com