users@jersey.java.net

Exception free way to use WebResources

From: tarjei <tarjei_at_nu.no>
Date: Wed, 15 Jul 2009 11:53:22 +0200

Hi,

Is there an exception free way to use WebResource?

I'm asking because I think that having to catch an exception to check
for a 404 when fetching an entity is too verbose.

It would be nice to either have a version of WebResource where it would
be possible to check return codes or where exceptions are thrown for
exceptional things like 500 codes.

Something like:

ret = webresource.path("/").get(SomeEntitiy.class);

if (ret == null) // resource returned 404
   doSomething();
else
   doSomethingElse();

Also, when doing a create, how do I get the location uri of the new object?

SourceView sv2 =
webResource.path("source").accept("application/xml").put(SourceView.class,
sv);

Ok, I now got the object, but I do not know the URI of the object.

One option would be to have the methods be able to return a response object:

Response r =
webResource.path("source").accept("application/xml").put(SourceView.class,
sv);

if (r.getStatus() == 204) {
...
}


This is a very typical REST client problem I think.

A HTTP response often contains more than just the entity you requested -
how this information should be delivered to the client in a clear way is
not always clear.

Any ideas?

Regards,
Tarjei


-- 
Tarjei Huse
Mobil: 920 63 413