Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making Json-Rpc calls from java

Tags:

java

json

rpc

I'm writing a java application which runs on the local machine, it needs to interact with another program (also running on the local machine), the other program has a JSON RPC API which is the best way to interact with it. However, in googling around I found a lot of libraries for exposing JSON RPC methods from a java application but nothing good on how to call a remote JSON method. How would I do this?

The two things I'm looking for are:

  1. A way to create new JSON objects to send
  2. A way to connect to the other program and send my response
  3. A way to decode the JSON response
like image 812
Martin Avatar asked Jul 14 '10 23:07

Martin


People also ask

Can RPC use JSON?

Based upon the JSON-RPC version used, the remote system will send various data outputs back to the source of request. All the JSON-RPC powered web transfers are unified and serialized with the help of JSON only.

What is JSON-RPC Java?

JSON-RPC is a transport-independent remote procedure call protocol using JSON as a data format. A call is represented by sending request or notification messages from the client to the server. A new protocol is defined by declaring requests, notifications and types used by the messages.

What is JSON-RPC call?

JSON-RPC is a remote procedure call protocol encoded in JSON. It is similar to the XML-RPC protocol, defining only a few data types and commands.


2 Answers

I have found couple of very good Java JSON-RPC libraries. Both are free. JSON-RPC 2 is of really high quality, and I can also recommend some of his other works. jsonrpc4j seems complete, but I have not used it. Both of these libraries solve several problems, so that you do not have to deal with low level implementation.

In addition, Google GSON is an awesome library for serializing Java objects into JSON. If you have a complicated API, it can help greatly.

like image 151
mikijov Avatar answered Oct 12 '22 04:10

mikijov


For interacting with JSON text in Java, see JSON in Java. Use this for building JSON request objects and serializing them to text as well as de-serializaing the response from the RPC server.

Is this over HTTP or a plain TCP connection? If the former, use HTTPClient. If the latter, just open a socket and use its I/O streams.

like image 36
Alan Krueger Avatar answered Oct 12 '22 06:10

Alan Krueger