users@jax-rs-spec.java.net

[jax-rs-spec users] Re: UriInfo CDI enabled

From: Christian Kaltepoth <christian_at_kaltepoth.de>
Date: Wed, 3 Feb 2016 06:38:40 +0100

@Sebastian:

AFAIK using @Context to inject JAX-RS objects into CDI beans works in
Jersey. Not sure about others implementations. And I don't think that this
is part of the spec.

Christian



2016-02-02 21:46 GMT+01:00 Sebastian Daschner <java_at_sebastian-daschner.de>:

> Hi Santiago,
>
> I mean injectable into a CDI managed bean not created by the JAX-RS
> runtime.
>
> So that it would be possible to write:
>
>
> @Stateless
> @Path("shopping_cart")
> public class ShoppingCartResource {
>
> @Inject
> BookstoreUriBuilder uriBuilder;
>
> @GET
> public ShoppingCart get() {
> ...
> uriBuilder.forBook(book);
> ...
> }
>
> }
>
>
> // Dependent scoped CDI bean
> public class BookstoreUriBuilder {
>
> @Inject
> UriInfo uriInfo;
>
> public URI forBook(Book book) {
> return uriInfo...
> }
>
> }
>
>
> Injection of UriInfo with @Context is only possible into JAX-RS
> resources -- according to Chapter 3.2 -- or am I missing something here?
>
> Cheers,
> Sebastian
>
>
> On 02/02/2016 03:01 PM, Santiago Pericasgeertsen wrote:
> > Sebastian,
> >
> > UriInfo should already be injectable using @Context.
> >
> > — Santiago
> >
> >> On Feb 1, 2016, at 3:56 PM, Sebastian Daschner <
> java_at_sebastian-daschner.de> wrote:
> >>
> >> Hi experts,
> >>
> >> Thanks for the work done so far.
> >>
> >> We had the case in projects that we wanted to construct URIs based on
> >> UriInfo without wanting to pass the current UriInfo into other
> >> (dedicated helper) classes.
> >>
> >> Do you think it's feasible to enable the request scoped UriInfo (plus
> >> maybe others) for CDI injection, in order to decouple potential URI
> >> construction classes from the JAX-RS resources?
> >>
> >> So something like follows would be possible (to call the method from any
> >> component):
> >>
> >> public class BookstoreUriBuilder {
> >>
> >> @Inject
> >> UriInfo uriInfo;
> >>
> >> public URI forBook(Book book) {
> >> return uriInfo.getBaseUriBuilder()
> >> .path(BooksResource.class)
> >> .path(BooksResource.class, "getBook")
> >> .build(book.getId());
> >> }
> >>
> >> }
> >>
> >> WDYT?
> >>
> >> Cheers,
> >> Sebastian
>
>


-- 
Christian Kaltepoth
Blog: http://blog.kaltepoth.de/
Twitter: http://twitter.com/chkal
GitHub: https://github.com/chkal