Java-json-rpc library

Overview

Java-json-rpc is library implementing JSON-RPC 2.0. Specification proposal can be found at here. Reason for this library is lack of good JSON-RPC implementations in java, especially on server side. It is distributed under GPL license. Library has implemented HTTP(using servlets)/TCP server and very simple HTTP/TCP clients.

javadoc.

Features

Java-json-rpc uses as definition of JSON-RPC services service class implementations, all that is needed to do is to add class to config of JsonRpcServlet in web.xml and that's it.

Installation - HTTP SERVER

Calling with named parameters

JSON-RPC specification allows sending parameters as map. Unfortunately java doesn't by default name parameters of methods. One way is to always send parameters in the correct order and server will ignore parameter names. Another approach is to annotate every parameter of service method with annotation cz.eman.jsonrpc.shared.ParameterName which will name it. Example:
	public void count(@ParameterName("car") Car car, @ParameterName("house") House house) { ... }

TCP Server example

Warning! Api changed and now TcpJsonSingleServer/TcpJsonMultiServer accept as first argument INSTANCE, not class of service.

There are currently 2 implementations of TCP server - single and multithreaded. They are both used same way, just multithreaded can work with more clients at once. To start multithreaded server use:
new TcpJsonMultiServer(new WebService(), 20000);
Where WebSerivce is implementation of service:
public class WebService implements IMyService {
	public Integer add(Integer a, Integer b) {
		return a + b;
	}

	public B echo(B b) {
		return b;
	}
}
To start singlethreaded server use:
new TcpJsonSingleServer(new WebService(), 20000);

HTTP/TCP Client example

If you have interface of webservice, for example like this:
public interface IMyService {

	Integer add(Integer a, Integer b) throws IOException;

	B echo(B b) throws IOException;

}
If you want to create client for this service, you must extend class from AbstractClientProxy:
public class WebServiceClientProxy extends AbstractClientProxy<IMyService> implements IMyService {

	public WebServiceClientProxy(ClientProvider clientProvider) {
		super(IMyService.class, clientProvider);
	}

	@Override
	public B echo(B b) throws IOException {
		return (B) super.callMethod("echo", b);
	}

	@Override
	public Integer add(Integer a, Integer b) throws IOException {
		return (Integer) super.callMethod("add", new Object[] { a, b });
	}

}
Note that it isn't necessary for thi class to implement IMyService or any, but it is better for code. All you have to do is to map parameters like as shown in method add and echo. Also don't use primitive type, use their wrappers instead.
Now that you have proxy, you need ClientProvider. If you want http client, use proxy like this:
WebServiceClientProxy proxy = new WebServiceClientProxy(new HttpJsonClient(new URL("http://localhost/json-rpc-testserver/jsonrpc/test1")));
System.out.println(proxy.add(1, 2));
System.out.println(proxy.echo(new B()));
If you want tcp client:
WebServiceClientProxy proxy = new WebServiceClientProxy(new TcpJsonClient(new Socket("localhost", 20000)));
System.out.println(proxy.add(1, 2));
System.out.println(proxy.echo(new B()));

TODO

Contact

You can contact me at my gmail account hovi6337 or project open discussion. I will welcome any feedback and/or comments :)