users@jax-rs-spec.java.net

[jax-rs-spec users] [jsr339-experts] is get(Class<T>) really a convenience?

From: Bill Burke <bburke_at_redhat.com>
Date: Mon, 11 Feb 2013 09:05:52 -0500

I've come across this while explaining the Client API in my book. Just
something to think about...

Is SyncInvoker.get(Class<T>) style of invocations really that
convenient? In order to make sure the response is closed, there's a bit
of extra code you have to write. All you're saving from this style is a
call to readEntity(). Other than that, its the same amount of code.


   Response response = target.request().get();
   try {
      if (response.getStatus() == 200) {
         String str = response.readEntity(String.class);
      }
   } finally {
      response.close();
   }

vs.

try {
   String str = target.request().get(String.class);
} catch (WebApplicationException wae) {
   wae.getResponse().close();
} catch (ResponseProcessingException rpe) {
   rpe.getResponse().close();
}

With the async API its even worse:

Future<Response> future = target.request().async().get();
Response response = future.get();
   try {
      if (response.getStatus() == 200) {
         String str = response.readEntity(String.class);
      }
   } finally {
      response.close();
   }

vs.

Future<String> future = target.request().async().get(String.class);
try {
    String str = future.get();
} catch (ExecutionException ex) {
    if (ex.getCause() instanceof WebApplicationException) {
       WebApplicationExceptoin wae = (WebApplicationException)ex.getCause();
       wae.getResponse().close();
    } else if (ex.getCause() instanceof ResponseProcessingException {
       ResponseProcessingException rpe =
(ResponseProcessingException)ex.getCause();
       rpe.getResponse().close();
    }
}



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