Play! as REST client

506 views
Skip to first unread message

Tristan Wehrmaker

unread,
Nov 8, 2009, 5:25:42 PM11/8/09
to play-framework
Hi!

I want to develop an application that acts as client for "RESTlike"
services like Flickr. At the moment I'm using the jersey client library.
Flickr, for example, provides XML and JSON representations.

What are the prefered ways to implement a REST client with Play!? How
to parse the XML or JSON responses of the REST sevices?

I can only find information about how to act as RESTful service and
how to provide XML or JSON representations but not how to consume them.

Thanks,
Tristan

Tristan Wehrmaker

unread,
Nov 8, 2009, 5:50:42 PM11/8/09
to play-framework
Hi!

I want to develop an application that acts as client for "RESTlike"
services like Flickr. At the moment I'm using the jersey client
library.
Flickr, for example, provides XML and JSON representations.

What are the prefered ways to implement a REST client with Play!? How
to parse the XML or JSON responses of the REST sevices?

I can only find information about how to act as a RESTful service and

Kasim

unread,
Nov 9, 2009, 12:09:48 AM11/9/09
to play-framework
Hi, I spent couple weeks playing with Play. Based on my limited
knowledge, I believe you can use JavaScript
Ajax Library to post call back to Play controller action methods. For
Ex:
1. Receive JSON or XML web service response
2. Use JS lib such as jQeuery to post Play Controller action method.
Play will map parameters to Java Objects.
3. Render using play controller to show the result.

This is just a possible idea to came to my mind after reading your
post. Hope you find it helpful.

Kasim

Erwan Loisant

unread,
Nov 9, 2009, 3:27:18 AM11/9/09
to play-fr...@googlegroups.com
Hi,

AFAIK Play only provides tools to make a server, so for the client
part I think you're fine using the Jersey library or any library you
like.

You can also use service-specific libraries such as flickrj or jickr
for Flickr for example.


--
Erwan Loisant

Tristan Wehrmaker

unread,
Nov 9, 2009, 4:26:35 AM11/9/09
to play-fr...@googlegroups.com
Hi,

the Jersey client library is what I'm currently using and it feels
really straightforward.

The service-specific are no alternatives for me, because I have to
integrate at least 10 services. So I want a generic interface.

My problem is now how to handle the XML and JSON stuff I'm getting as
response from the services. Possible ways are DOM, JDOM, SAX, StAX,
JAXP, JAXB. I can't find a way out of this tangle.
Clientside "posting" (as Karim mentioned) is not possible for me because
my app offers a RESTful service, where in most cases no browsers are
involved.

Erwan Loisant schrieb:

Erwan Loisant

unread,
Nov 9, 2009, 4:48:35 AM11/9/09
to play-fr...@googlegroups.com
Hi,

On Mon, Nov 9, 2009 at 10:26, Tristan Wehrmaker <3S...@3stan.net> wrote:
> the Jersey client library is what I'm currently using and it feels
> really straightforward.
>
> The service-specific are no alternatives for me, because I have to
> integrate at least 10 services. So I want a generic interface.

I see.

> My problem is now how to handle the XML and JSON stuff I'm getting as
> response from the services. Possible ways are DOM, JDOM, SAX, StAX,
> JAXP, JAXB. I can't find a way out of this tangle.
> Clientside "posting" (as Karim mentioned) is not possible for me because
> my app offers a RESTful service, where in most cases no browsers are
> involved.

It's mostly a matter of preference. As far as I'm concerned, I prefer
to work with JSON for various reasons so I do so when I can. Then of
course some services will only provide you responses in XML. In this
case:
* SAX and its friends are event-based, so you can work on an XML
Stream while DOM requires you to have to whole document before working
on it.
* DOM and JDOM do a full parsing, so it's easier to work with.

In your case I don't think SAX is worth the pain, since you'll have to
parse the whole document before generating the template anyway. So I
think you should use DOM or JDOM. DOM is based on the W3C standard, so
if you're used to it already (because you used it in Javascript) maybe
it's better for you. JDOM is designed to be a simpler API so if you're
not used to DOM already it could be better for you.

Look at the code samples, see which one you prefer!


--
Erwan Loisant

Guillaume Bort

unread,
Nov 9, 2009, 5:04:20 AM11/9/09
to play-fr...@googlegroups.com
To parse XML you can find a useful XPath based lib in the play.libs package.

Tristan Wehrmaker

unread,
Nov 9, 2009, 4:19:13 PM11/9/09
to play-fr...@googlegroups.com
Thanks a lot to Erwan and Guillaume.

I switched to JSON responses from Flickr now.
When I use play.data.parsing.JsonParser to parse the JSON string, I
get a map, whose keys look like this:

[
contacts.contact[0].nsid,
contacts.contact[0].realname,
contacts.contact[0].username,
...
contacts.contact[1].nsid,
contacts.contact[1].realname,
contacts.contact[1].username,
...
contacts.per_page,
contacts.total,
stat,
]

How can I iterate over the contacts? Or am I working with the wrong
parser?

It would be nice if there is a structure I can walk through like
System.out.println(rsp.contacts.total)
or
for (Node contact : rsp.contacts.contact) {...}


Greets,
Tristan

Guillaume Bort

unread,
Nov 9, 2009, 4:21:30 PM11/9/09
to play-fr...@googlegroups.com
Do you use the play.libs.WS API ?

Tristan Wehrmaker

unread,
Nov 9, 2009, 4:26:57 PM11/9/09
to play-fr...@googlegroups.com
Wow!

I thought play.libs.WS is for non-real "Webservices" like SOAP and so
on.

But this API is for real webservices. That's great. Is there any
documentation about using it?

Thanks,
Tristan

Guillaume Bort

unread,
Nov 9, 2009, 4:45:51 PM11/9/09
to play-fr...@googlegroups.com
Not so difficult,

For example, using a GET url from flickr:

WS.url("flickrURLhere").get().getJson()

Then you get a GSON object.
Reply all
Reply to author
Forward
0 new messages