Wednesday, April 27, 2011

Creating a JSON-RPC service using Maven, Eclipse, and Tomcat

Oddly, there are very few resources for creating JSON-RPC services implemented using Java. JSON-RPC is a nice way for heterogeneous servers to communicate using a lightweight protocol. Because of the hierarchical nature of JSON plus its readiness for easy conversion to native domain objects, one would expect it to be a POJO by now.

However, that is not the case, and implementation requires download of third-party libraries such as jsonrpc4j. The site suggests that you create a Maven project to link to their JARs, write a couple of classes, and you magically have a JSON-RPC service. I beg to differ.

Setup

First, you need to create a Maven project, which is a macro available in SpringSource Tool Suite. Select File > New > Project... > Maven Project. Most importantly, you get a pom.xml and a /src/main directory with this macro. The Apache Web site specifies that directories in a Maven project are specified by convention. Therefore, to conform to Maven convention, you need to add a java/ directory under /src/main.

If you are a newbie to Spring, simply following the instructions in the site are not enough. In addition, you must add:

<servlet>
<servlet-name>remoting</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>remoting</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>

to your web.xml. In addition, the XML file containing the properties should be named remoting-servlet.xml. Again, this is according to Spring convention.

You will need to add the following line to your interface:

import com.googlecode.jsonrpc4j.JsonRpcParamName;

and you may need to fiddle with pom.xml a little bit (I had to force Maven to use Java 1.6). In the meantime, Eclipse will likely display syntax errors but you may need to ignore them.

Compiling

The first time you attempt to compile using Maven, you should create a Run Configuration. To get the Base Directory, Browse the Workspace and select the project name. Under Goals, enter "eclipse:eclipse". Run the project once. If it succeeds, the only thing you've done is to force Eclipse to load the JARs declared in pom.xml. The syntax checker should be accurate from this point on.

Change the Goals to "clean install". As long as you don't declare new JARs, you should not need to change your Run Configuration again. Select this configuration to build your WAR file.

Deploying

Assuming you have a Tomcat instance available to you (I installed one on Ubuntu using "sudo apt-get install tomcat6"). Copy the WAR file to /var/lib/tomcat6/webapps/.

Testing

Because this is a JSON-RPC service, the most appropriate way to test is to use the following cURL command:

curl -v http://localhost:8080/projname/myServlet -d @test.json

where test.json may look like:

{
"jsonrpc": "2.0",
"method": "createUser",
"params":[
{"firstName": "Dean"}
],
"id": "0"
}

If it works, you are supposed to get a JSON response back, representing the deserialized Java object.

However, it didn't work for me. Yet.