Monday, January 2, 2012

GWT with JAX-RS (aka RPC/REST) Part 2


Come unto me all ye who are weary of heavy-laden clients and I will give you REST.

Continued from GWT with JAX-RS (aka RPC/REST) Part 1


We define the lower level REST API on an interface commonly shared between the client and server. We should consider the REST API as the lower level API, while considering the plain old Java object API (POJO-API) as the higher level API. For the purpose being lazy, I would like to use the acronym POJO-API henceforth.

Since the Java interface of the REST API is shared, both client and server share the same javadoc and json object documentation.

For example, in the interface declaration:

@Path("/hello/info")
interface RemoteCall{
  @Path ("/services/greet")
  @POST
  Person getGreeting(String userName, Message msg );
}

The client invokes the call by
Person veraswamy = getGreeting(String userName, Message msg );

The server listens for the path signature of `/hello/info/services/greet` and maps it to executing the mapped POJO-API by obtaining an instance of RemoteCall from the jax-rs-ware:

remotecalls.getGreeting(String userName, Message msg );

The server-side jax-rs-ware executes the impl of the method and returns Person as a response. The client-side of the jax-rs-ware decodes the response and the variable obama is assigned the value of Person as executed on the server-side.

See -no hands-

The programmer consuming the Java API is oblivious to the underlying REST API, but simply uses the POJO methods with a purely RPC attitude. Meanwhile, the REST API is designed by the developer providing the API, who would need to ensure uniqueness of the polymorphic signature of the API.

For async calls, we run a simple text utility script over the interface methods to produce the reciprocal async-client interface. So that the getGreeting method becomes

interface AsyncRemoteCall{
  @Path ("/services/greet")
  @POST
  void getGreeting(String userName, Message msg, CallBack<person> callback );
}

So, not only do the client and server share the interface, they also share the POJO DTOs, as long as the DTOs are serializable.

This is not exactly json-rpc as JSON-RPC has usually been done. But, this is RPC nonetheless. And the POJOs are carried as JSON over the wire. OTH, this is also a form of XML-RPC over JAX-RS, because we can switch the codec from JSON to XML. In fact, we can mix both XML-RPC and JSON-RPC together on the same server, using REST as the transport. May be, what I am doing should be called JSON-RPC (or XML-RPC) over REST-RPC.

I can say that my implementation of JSON-RPC over JAX_RS allows me to share the same set of POJO interfaces, DTO and documentation between client and server.

I could run enunciate over the interface to obtain the json object documentation (or the XML-schema). So, if you choose to, you could use PHP on the server side servicing the remote procedure call.