Howto: RPC with Http GET

48 views
Skip to first unread message

George Georgovassilis

unread,
Aug 28, 2010, 4:29:18 AM8/28/10
to Google Web Toolkit
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

mmoossen

unread,
Aug 28, 2010, 10:37:13 AM8/28/10
to Google Web Toolkit
good hint!

thanks for sharing
Michael

On Aug 28, 11:29 am, George Georgovassilis
> [1] Yahoo Exceptional Performance Blog, Use GET for Ajaxhttp://developer.yahoo.com/performance/rules.html
>
> [2] GWT RPC calls with Http GET methodhttp://georgovassilis.blogspot.com/2010/08/gwt-rpc-calls-with-http-ge...
Reply all
Reply to author
Forward
0 new messages