2008/11/06

Generate RequestBuilder logic using RestGWT

Besides GWT's RPC, GWT also allows user to communicate backend web services using standard HTTP calls. The latter, comparing with RPC, is much more flexible and supports arbitry payload formats.

However, coding with GWT HTTP calls (the RequestBuilder) is a redundant and repeating work. Besides worrying about preparing the payload content, developers also need to write lots of lines of code to prepare the instance of RequestBuilder itself, to worry about metadata (http headers) used for it, to care of the URL of interface and the method of the HTTP call.

This does not sound too much a big deal if the backend service is sort of traditional RPC style, in which the interface (the URL) of endpoint is usually fixed and these metadata used by HTTP calls are usually unchanged. For a RESTful style service, things might become a lit more complex, in which

  • Each resource is unique identified by a URI;
  • Same kind of resources' URIs conforms to same pattern;
  • Metadata such as HTTP headers usually instruct how the backend service to process this request and impact the representation of the response;
A bit helter-skelter, isn't it?

Present you RestGWT, the generator for RequestBuilder logic

RestGWT is a GWT generator implementation, particular for simplifying coding work about GWT HTTP Calls, specifically, HTTP Calls to a RESTful service.

RestGWT steals some concepts and nice ideas from JAX-RS, WADL and Restlet, says for making a HTTP Calls you will need
  • An @Endpoint which specifies the uri template in the Restlet style, i.e. /ci/{type}/{id} and the method used to do the call
  • One or more @HttpHeaders indicates metadata, i.e @HttpHeader(name="Content-Type", value="application/ci+xml)"
  • Apply above two items on a method of an interface, i.e. void getCI(String type, String id, RequestCallback callback);
  • @UriPart can be used to indicate how to replace url template with values specified by method parameters, so the method signature now becomes void getCI(@UriPart("type") String type, @UriPart("id") String id);
  • Finally, the interface containing the method needs extends interface org.edgebox.gwt.rest.client.RestRemote as a flag;
Up-to-now, an endpoint to do a HTTP Call is declared as

public interface MyService extends RestRemote{

@Endpoint(method=RestGWT.Method.Represnt, urlTemplate="/a/ci/{type}/{id}")
@HttpHeader(name="Content-Type", value="application/ci+xml")
public void getCI(@UrlPart("type") String type,
@UrlPart("id") String id, RequestCallback, callback);
}
Now, to use this service, using GWT RPC style:
MyService service = GWT.create(MyService.class)
RequestCallback callback = //...
service.getCI("person", "111", callback)
Is this a nice cool idea? I am not quite sure, but at least it suits my requirement pretty well. At current moment, RestGWT is still under heavy working. But you can get source code from http://edgebox.googlecode.com/svn/trunk/edgebox-restgwt for a look.

1 comments:

Олександр 25/10/09 18:23  

Can you please provide examples of POSTing data with your framework? How to deal with larger sets of parameters (up to 10-15 fields)? Can ReqData be used (if yes - how)? Thanks in advance.

Footer

  © Blogger template 'Grease' by Ourblogtemplates.com 2008

Back to TOP