-
Notifications
You must be signed in to change notification settings - Fork 66
Resty examples
Welcome to the Resty for Java wiki!
For now, I will collect some examples how to use Resty here.
Getting location information from the geonames web service, published as JSON:
Resty r = new Resty(); Object name = r.json("http://ws.geonames.org/postalCodeLookupJSON?postalcode=66780&country=DE"). get("postalcodes[0].placeName");
This gets a JSON object from the specified URL and extracts the first place name.
Getting the Google Developer calendar feed as JSON and following the first entry, which is an XML resource, extracting the title tag:
Resty r = new Resty(); String title = r.json("http://www.google.com/calendar/feeds/[email protected]/public/full?alt=json"). xml(path("feed.entry[0].id.$t")).get("entry/title/text()", String.class);
The path(...) expression is used to extract a URL from the returned JSON object, which is then used to read an XML document.
Getting ATOM feed from Slashdot and printing article URLs:
Resty r = new Resty(); NodeList nl = r.xml("http://rss.slashdot.org/Slashdot/slashdotGamesatom").get("feed/entry/link"); for (int i = 0, len = nl.getLength(); i < len; i++) { System.out.println(((Element)nl.item(i)).getAttribute("href")); }
Some supported JSON path constructs:
store.book[price>7 && price<12.999].author store.book[!category='reference'].author
JSON Sample for paths above:
{ "store": { "book": [ { "category": "reference", "author": "Nigel Rees", "title": "Sayings of the Century", "price": 8.95 }, ... ]}}
Chaining calls to navigate JSON objects. This is useful if the JSON contains URIs to go down the rabbit hole so to say:
import static de.monoid.web.Resty.*; import de.monoid.web.Resty; JSONObject json = r. json("http://localhost:9999/rest/sc"). json(path("serviceclients[displayName='Sample'].href")). json(path("workflows")).json(path("current")).json(path("levels[displayName='Incoming'].href")). json(path("ruleSets[1].EngageRouting")).object();