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.
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.
<servlet> <servlet-name>JsonRpcServlet</servlet-name> <display-name>JsonRpcServlet</display-name> <servlet-class>cz.eman.jsonrpc.server.JsonRpcServlet</servlet-class> <init-param> <param-name>test1</param-name> <param-value>cz.eman.jsonrpc.server.example.WebService</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>JsonRpcServlet</servlet-name> <url-pattern>/jsonrpc/*</url-pattern> </servlet-mapping>Class must have public default constructor, all public non-static classes will be exposed. Complex types in methods must have public default constructor.
public void count(@ParameterName("car") Car car, @ParameterName("house") House house) { ... }
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);
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.
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()));