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:
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With