Hi,
On 07/15/2009 12:22 PM, Paul Sandoz wrote:
> Hi,
>
> On Jul 15, 2009, at 11:53 AM, tarjei wrote:
>
>> 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();
>>
>
> You can do:
>
> try {
> ...
> } catch (UniformInterfaceException ex) {
> ClientResponse cr = ex.getResponse();
> if (cr.getStatus() == 404) { ... }
> }
>
>
>> 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.
>>
>
>
> You can do:
>
> ClientResponse cr = webResource...put(ClientResponse.class, sv);
> if (cr.getStatus() == 201) {
> URI u = cr.getLocation();
> SourceView sv = cr.getEntity(SourceView.class);
> }
>
>
> However, there is a slight inconsistency in that returning a
> ClientResponse will not result in a UniformInterfaceException exception
> being throw for status codes >=300. You can rectify this by adding a
> client filter that supports such behavior.
Hmm, If I understood this correctly, then this is the behaviour I'm
looking for - i.e. that I have to check the returned code to know if I
got a correct response.
Thanks to both you and Mr. Probst for quick replies. After rereading the
javadocs, I see that it is actually plainly visible there as well. I
should have seen that earlier.
Kind regards,
Tarjei
>
>
> If only Java could return multiple values and the types of those could
> be reflected on a runtime, then it would be easier to define the tuple
> of entity and location.
>
> It is hard to specify a generic solution like returning
> Created<SourceView> because it is not easy for the developer to declare
> references to Java types. But it should be possible to define a message
> body reader for a type Created, from which the entity can be obtained
> from getEntity. If the status code is not 201 a
> UniformInterfaceException can be thrown.
>
> Paul.
>
>> 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
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
>> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>
--
Tarjei Huse
Mobil: 920 63 413