Sunday, June 25, 2006

REST

I am currently looking into network APIs for search engine, blogs, social bookmarking services etc and realised that there seeems to be a trend away from WebServices towards a simpler protocol called REST. The definitions are a little bit fuzzy here, and it looks like some of the stuff I have implemented as part of WOP (wopper-rpc module, on SF-subversion) can be described as restful:
  • I have started with an XML Schema
  • Communication is by plain POST requests
  • I submit objects encoded as instances of the schema to the server, and get another encoded object back as part of the response
  • The objects are actually Java objects, but this is irrelevant, I have generated the classes from the schema using Jaxb.
  • I am using the Apache commons client to do the plumbing .
Here some code, just to see how simple it is:

Object obj = ... ; // the object to be posted
HttpClient client = new HttpClient();
StringWriter stringWriter = new StringWriter();
// serialize object, uses the generated API !!
WopperRPCUtil.write(stringWriter,object);
// make request
PostMethod post = new PostMethod(url);
post.setRequestEntity(new StringRequestEntity(stringWriter.toString()));
int statusCode =statusCode = client.executeMethod(post);

No comments: