users@jersey.java.net

Re: One Resource class for two URLs

From: Martin Probst <mail_at_martin-probst.com>
Date: Tue, 22 Apr 2008 19:10:43 +0200

> Now that i have a really clear idea about what you are trying to do
> I am wondering if you can do what you require with a
> ResourceProvider. ResourceProvider's are responsible for
> instantiating resource classes and managing the life-cycle, often
> they defer to the component provider. They are like factories for
> resources.

Hey, that's what I meant with the factory for resources. I admit that
I asked my question in the probably most confusing way possible :-)

I'll try that out and update my article accordingly.

> But this will not work for the POST method you presented in the
> Books class. However, you could do this:
>
> Book b = new Book();
> provider.inject(b); // Perhaps you don't need to inject for using
> Book as an representation ?
> book.setFields(...);
> session.persist(book);
> return book;

I will need to inject, as I currently plan to have a "getURI()" method
in all my resources, so I can easily link to them in the views. And
that of course needs a UriInfo. Or does it? I only need the UriInfo
for host, port and scheme - simply instantiating a UriBuilder gives
URLs like "//:-1/...". Is that a bug (I think I've seen it behave
differently)? If yes, how would I create a relative link?

> One way to avoid this is to return the URI to the book you created.
> Use Response.created(URI). For browsers you may have to do a
> redirect (303 i think using Response.seeOther). The disadvantage is
> of course that it requires another GET by the client.

I think it's accepted to do it this way. Is there a recommended way to
separate the two cases - Created vs. See other for browsers vs.
automated clients?

Regards,
Martin