Thursday, December 10, 2009

JSON-RPC between .NET service and Java client

Recently i experimented with Jayrock .NET library for JSON-RPC and following its tutorial it was very simple to run a .NET JSON-RPC service on my Win7 Home Premium (IIS 7.5). From my earlier experience with JSON-RPC in Java, i wrote a Java client using json-rpc-client library and could get the RPC communication happening without any issue.

The ease with which the code samples worked for the below helloworld-ish sample, motivated me to write about it. JSON-RPC is simpler to debug and implement than SOAP and can be secured by implementing it over https. Most of the time in systems integration all we need is getting the .NET software to communicate with the Java software and in such simpler scenarios where RPC mechanism which is firewall friendly like SOAP is needed then thats where JSON-RPC is a good option.

helloworld.ashx - the JSONRPCHandler

using System;
using System.Web;
using Jayrock.Json;
using Jayrock.JsonRpc;
using Jayrock.JsonRpc.Web;

namespace jayrockserv
public class HelloWorld : JsonRpcHandler
public string Greetings()
return "Welcome to Jayrock!";

public int sum(int a, int b)
return (a+b);


The Java JSON-RPC client:

import org.apache.commons.httpclient.HttpState;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.codebistro.jsonrpc.Client;
import org.codebistro.jsonrpc.HTTPSession;
import org.codebistro.jsonrpc.TransportRegistry;

public class Main {
private static final Log log = LogFactory.getLog(Main.class);
private static final String rootURL= "http://localhost/jayrock/helloworld.ashx";
* @param args
public static void main(String[] args) {
// Register HTTP
HTTPSession httpSession= (HTTPSession)TransportRegistry.i().createSession(rootURL);
HttpState state= new HttpState();
Client client= new Client(httpSession);
RpcIF rpc = client.openProxy(null, RpcIF.class);
String str = rpc.greetings();
log.debug("Output:" + str);

public interface RpcIF {
String greetings();

O/P on client side:

JSON RPC REQUEST ==> {"id":1,"method":"greetings","params":[]}
JSON RPC RESPONSE ==> {"id":1,"result":"Welcome to Jayrock!"}

No comments:

15 sorting algorithms visualized in 5 minutes, with awesome arcade sounds

15 sorting algorithms visualized in 5 minutes, with awesome arcade sounds from r/programming