users@jersey.java.net

WebResource vs WebResource.Builder

From: Tyson Norris <tyson.norris_at_marketlive.com>
Date: Fri, 25 Jul 2008 13:52:00 -0700

Hi All -

In a first attempt at creating a java based client using Jersey 0.8
with:

com.sun.jersey.api.client.Client

com.sun.jersey.api.client.WebResource

 

 

I noticed that running the following works:

WebResource wr = c.resource("http://localhost:8081/Test/resources/");

String jsonData =
wr.path("foo/").accept("application/json").get(String.class);

 

 

But this does NOT work:

WebResource wr = c.resource("http://localhost:8081/Test/resources/");

wr.path("foo/");

String jsonData = wr.accept("application/json").get(String.class);

 

I did also try this which DOES work:

WebResource wr = c.resource("http://localhost:8081/Test/resources/foo");

String jsonData =wr.get(String.class);

 

It seems like WebResource.get() will only work if there is no need to
use the WebResource.accept() or WebResource.path() methods. If accept()
and path() are required, then you MUST execute WebResource.Builder.get()
(instead of WebResource.get()).

 

Is this all correct? I got very confused when I changed syntax from the
chained-method syntax to separate statements, and then realized that the
methods return WebResource.Builder when I assumed it returned
WebResource.

 

Thanks!

Tyson