Saturday, August 28, 2010

Howto: RPC with Http GET

There've been some requests to perform RPC invocations with HTTP GET,
as the response is cacheable by the browser (and if you believe the
word on the net, it's faster [1] than POST).

I've written a wrapup on how to get it done [2], the essentials in a
nutshell:

Given a service you want to make GETable:

interface GeoService{
String[] getListOfPlaces(String countryCode);
double[] getGeoCoordinatesForPlace(String placeCode);
}

On the client side, you need to provide the service proxy with a
request builder that performs GET requests:


public void getGeoCoordinates(final String placeCode, final
AsyncCallback callback){

((ServiceDefTarget)geoService).setRpcRequestBuilder(new
RpcRequestBuilder(){
@Override
protected RequestBuilder doCreate(String serviceEntryPoint) {

return new RequestBuilder(RequestBuilder.GET, "/myapp/geoservice?
placecode="+placeCode);

}
});
geoService.read(placeCode, callback);
}


On the server side, you need to create a servlet that does _not_
extend RemoteServiceServlet but uses the GWT RPC utilities directly:

public void doPost(HttpServletRequest request, HttpServletResponse
response)throws Exception{
String placeCode = request.getParameter("placecode");
Method method = geoService.getClass().getMethod("read", new
Class[]{String.class});
double[] coordinates = geoService.read(placeCode);
String sPayload = RPC.encodeResponseForSuccess(method,
coordinates);
byte[] payload;
payload = sPayload.getBytes("utf-8");
response.setHeader("Content-Disposition", "attachment");
response.setContentType("application/json;charset=utf-8");
response.setContentLength(payload.length);
ServletOutputStream stream = response.getOutputStream();
stream.write(payload);
}


[1] Yahoo Exceptional Performance Blog, Use GET for Ajax
http://developer.yahoo.com/performance/rules.html

[2] GWT RPC calls with Http GET method
http://georgovassilis.blogspot.com/2010/08/gwt-rpc-calls-with-http-get-method.html

--
You received this message because you are subscribed to the Google Groups "Google Web Toolkit" group.
To post to this group, send email to google-web-toolkit@googlegroups.com.
To unsubscribe from this group, send email to google-web-toolkit+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-web-toolkit?hl=en.

No comments:

Post a Comment