<?xml version="1.0" encoding="UTF-8"?> <rss
version="2.0"
xmlns:content="http://purl.org/rss/1.0/modules/content/"
xmlns:wfw="http://wellformedweb.org/CommentAPI/"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:atom="http://www.w3.org/2005/Atom"
xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
><channel><title>Werx Limited &#187; war</title> <atom:link href="http://werxltd.com/wp/tag/war/feed/" rel="self" type="application/rss+xml" /><link>http://werxltd.com/wp</link> <description>We make IT work.</description> <lastBuildDate>Mon, 23 Jan 2012 23:03:59 +0000</lastBuildDate> <language>en</language> <sy:updatePeriod>hourly</sy:updatePeriod> <sy:updateFrequency>1</sy:updateFrequency> <generator>http://wordpress.org/?v=3.3.1</generator> <item><title>Simple Java implementation of JSON-RPC</title><link>http://werxltd.com/wp/2009/12/29/simple-java-implantation-of-json-rpc/</link> <comments>http://werxltd.com/wp/2009/12/29/simple-java-implantation-of-json-rpc/#comments</comments> <pubDate>Tue, 29 Dec 2009 12:00:49 +0000</pubDate> <dc:creator>wes</dc:creator> <category><![CDATA[java]]></category> <category><![CDATA[software development]]></category> <category><![CDATA[api]]></category> <category><![CDATA[design pattern]]></category> <category><![CDATA[json]]></category> <category><![CDATA[json-rpc]]></category> <category><![CDATA[maven]]></category> <category><![CDATA[pom]]></category> <category><![CDATA[sdk]]></category> <category><![CDATA[software]]></category> <category><![CDATA[war]]></category> <category><![CDATA[xml]]></category><guid
isPermaLink="false">http://werxltd.com/wp/?p=323</guid> <description><![CDATA[Preamble Explanation of standard formats and protocols: JSON (JavaScript Object Notation) is a lightweight1 data-interchange format with language bindings for C, C++, C#, Java, JavaScript, Perl, TCL and many others. JSON-RPC is a simple remote procedure call protocol similar to XML-RPC although it uses the lightweight JSON format instead of XML. What is it? I [...]]]></description> <content:encoded><![CDATA[<h2>Preamble</h2><p>Explanation of standard formats and protocols:</p><ul><li><strong><a
href="http://www.json.org/">JSON</a></strong> (JavaScript Object Notation) is a lightweight<sup><a
href="http://werxltd.com/wp/2009/12/29/simple-java-implantation-of-json-rpc/#footnote_0_323" id="identifier_0_323" class="footnote-link footnote-identifier-link" title="Lightweight in both size and resources required to process data encoded in JSON vs. XML.">1</a></sup> data-interchange format with language bindings for C, C++, C#, Java, JavaScript, Perl, TCL and many others.</li><li><strong><a
href="http://json-rpc.org/">JSON-RPC</a></strong> is a simple remote procedure call protocol similar to <a
href="http://www.xmlrpc.com/">XML-RPC</a> although it uses the lightweight <a
href="http://www.json.org/">JSON</a> format instead of <a
href="http://en.wikipedia.org/wiki/XML">XML</a>.</li></ul><h2>What is it?</h2><p>I love the simplicity of <a
href="http://json-rpc.org/">JSON-RPC</a> when it comes to rapidly devloping cutting-edge web applications.</p><p>I recently decided to start writing more server-side code in <a
href="http://en.wikipedia.org/wiki/Java_Servlet">Java servlets</a> in order to take advantage of cloud-based infrastructures such as the <a
href="http://code.google.com/appengine/">Google App Engine</a>. Until now I have written my web applications using <a
href="http://php.net/">PHP</a> as the server-side language. Specifically using frameworks such as <a
href="http://www.symfony-project.org/">Symfony</a> or <a
href="http://www.kohanaphp.com/">Kohana</a>, which makes writing simple JSON-RPC services relatively trivial.</p><p>So I started looking for a simple JSON-RPC system I could use in Java to abstract my business logic classes from the mundane tasks associated with handling web requests. I found several packages which all claimed to implement the JSON-RPC protocol via Java servlets, however they seemed to require far more by way of setup than I wanted and they all seemed to require the developer to write applications to their specific implementation&#8217;s standards.<sup><a
href="http://werxltd.com/wp/2009/12/29/simple-java-implantation-of-json-rpc/#footnote_1_323" id="identifier_1_323" class="footnote-link footnote-identifier-link" title="I did find this project after finishing the first revision of my class. It looks great and like it would do much of what I wanted, however the code is proprietary. After I finish documenting my classes I plan on releasing them under an OSI-approved licence. If you are interested in helping me with this project, feel free to let me know!">2</a></sup></p><p>So I decided to write yet another Java implementation of the JSON-RPC specification myself with the following goals in mind:</p><ul><li>Easy to implement. Setup for this package should be kept at a minimum. This includes both development as well as production setup.</li><li>Easy to code in. Application developers using this class should not need to know much about JSON-RPC beyond exposing methods in their code that can be called remotely from other applications via the web.</li><li>Non-invasive. Developers using this implementation should be able to reuse <a
href="http://en.wikipedia.org/wiki/Plain_Old_Java_Object">plain old Java object</a> (POJO) classes as much as possible, making the transport layer of JSON-RPC as transparent as possible.</li></ul><p>With these goals in mind I set off to develop the package com.werxltd.jsonrpc to be a simple wrapper designed to be used inside of a <a
href="http://mindprod.com/jgloss/warfile.html">standard .war project</a>.</p><h2>Setting it up</h2><p>You can either <a
href="http://maven.werxltd.com/com/werxltd/jsonrpc/0.9/jsonrpc-0.9.jar">download the .jar file here</a> to include in your project manually or (and this is my preferred method) you can import the com.werxltd.jsonrpc package as a dependency in your Maven-managed project by specifying the following in your project&#8217;s <a
href="http://maven.apache.org/pom.html">pom.xml</a> configuration file:</p><pre class="brush:xml">&lt;repositories&gt;
    &lt;repository&gt;
        &lt;id&gt;werxltd&lt;/id&gt;
        &lt;url&gt;http://maven.werxltd.com &lt;/url&gt;
        &lt;snapshots&gt;
            &lt;enabled&gt;true&lt;/enabled&gt;
        &lt;/snapshots&gt;
        &lt;releases&gt;
           &lt;enabled&gt;true&lt;/enabled&gt;
       &lt;/releases&gt;
    &lt;/repository&gt;
&lt;/repositories&gt;

&lt;dependencies&gt;
    &lt;dependency&gt;
        &lt;groupId&gt;com.werxltd&lt;/groupId&gt;
        &lt;artifactId&gt;jsonrpc&lt;/artifactId&gt;
        &lt;version&gt;0.9&lt;/version&gt;
    &lt;/dependency&gt;
&lt;/dependencies&gt;</pre><p>Next, you&#8217;ll need to specify endpoints in your .war file&#8217;s <a
href="http://en.wikipedia.org/wiki/WAR_(Sun_file_format)">web.xml</a> configuration file. Here&#8217;s an example:</p><pre class="brush:xml">&lt;web-app&gt;
    &lt;servlet&gt;
        &lt;servlet-name&gt;example&lt;/servlet-name&gt;
        &lt;servlet-class&gt;com.werxltd.jsonrpc.RPC&lt;/servlet-class&gt;
        &lt;init-param&gt;
            &lt;param-name&gt;rpcclasses&lt;/param-name&gt;
            &lt;param-value&gt;YourClass&lt;/param-value&gt;
        &lt;/init-param&gt;
    &lt;/servlet&gt;

    &lt;servlet-mapping&gt;
        &lt;servlet-name&gt;example&lt;/servlet-name&gt;
        &lt;url-pattern&gt;/example&lt;/url-pattern&gt;
    &lt;/servlet-mapping&gt;
&lt;/web-app&gt;</pre><p>That&#8217;s it! Now your project is configured to filter all requests sent to /example through the JSON-RPC class which examines the class name you passed in (in this case, YourClass) for public methods it can expose. An instance of your class is created internally if your class is not static and it will remain in memory throughout the life of the servlet. Any exceptions your class generates are gracefully wrapped inside a JSON-RPC error message for proper handling upstream.</p><h2>Using it</h2><p>While the setup is pretty much straightforward, due to the loose typing found in JavaScript (and, as a result, JSON) there are some caveats in how methods are called. Specifically in how arguments are passed to those methods.</p><p>Scanned methods are stored internally with a signature consisting of the method name and how many arguements that method accepts. When a JSON-RPC request is made, the servlet determines how many parameters were included and attempts to match the method requested with a corresponding internal method which has the same number of parameters/arguements.</p><p>Because parameters are passed in via the web, only Java primitive data types along with three others, <a
href="http://www.json.org/javadoc/org/json/JSONObject.html">JSONObject</a>, <a
href="http://www.json.org/javadoc/org/json/JSONArray.html">JSONArray</a> and <a
href="http://java.sun.com/j2se/1.5.0/docs/api/java/lang/String.html">java.lang.String</a> are accepted as valid parameter data types.</p><p>If a match of method name and number of parameters is found, an attempt is made to parse the passed-in data into the method&#8217;s required type. Any methods which accept as their first parameter a parameter of the JSONObject type, this method automatically takes prescience over all other parameters.</p><p>To use the JSON-RPC interface from another application, you must pass a valid JSON-RPC object to your final servlet as a parameter named &#8220;json&#8221; via either GET or POST. Here is an example of the valid JSON-RPC object you need to pass:</p><pre class="brush:javascript">{
    "method":"add",
    "params":[1 2 3]
}</pre><p>This JSON-RPC implementation accepts either named parameters or positional parameters like the ones shown above. Here is a named parameters example:</p><pre class="brush:javascript">{
    "method":"echo",
    "params":{
        "text":"testing"
    }
}</pre><h2>The road ahead</h2><p>Future development of this class will include more formalized access between the JSON-RPC layer and the underlying classes.</p><p>I&#8217;m also planning to post the <a
href="http://en.wikipedia.org/wiki/Javadoc">Javadocs</a> and the source to an example project that utilizes the JSON-RPC transport package.</p><p>Hope this helps someone else. I&#8217;m looking forward to using this class as a central component in many <a
href="http://en.wikipedia.org/wiki/Rich_Internet_application">rich web projects</a> I have planned.</p><div
class="betterrelated none"><p>No related content found.</p></div><ol
class="footnotes"><li
id="footnote_0_323" class="footnote">Lightweight in both size and resources required to process data encoded in JSON vs. XML.</li><li
id="footnote_1_323" class="footnote">I did find <a
href="http://software.dzhuvinov.com/json-rpc-2.0-base-java-classes.html">this project</a> after finishing the first revision of my class. It looks great and like it would do much of what I wanted, however the code is proprietary. After I finish documenting my classes I plan on releasing them under an <a
href="http://www.opensource.org/licenses">OSI-approved licence</a>. If you are interested in helping me with this project, feel free to let me know!</li></ol><p><!--[if IE]><iframe
frameborder="0" allowTransparency="true" class="addtoany_special_service facebook_like" src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwerxltd.com%2Fwp%2F2009%2F12%2F29%2Fsimple-java-implantation-of-json-rpc%2F&amp;layout=button_count&amp;show_faces=false&amp;width=75&amp;action=like&amp;colorscheme=light&amp;height=20&amp;ref=addtoany" scrolling="no" style="border:none;overflow:hidden;width:90px;height:21px"></iframe><![endif]--><!--[if !IE]><!--><iframe
class="addtoany_special_service facebook_like" src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwerxltd.com%2Fwp%2F2009%2F12%2F29%2Fsimple-java-implantation-of-json-rpc%2F&amp;layout=button_count&amp;show_faces=false&amp;width=75&amp;action=like&amp;colorscheme=light&amp;height=20&amp;ref=addtoany" scrolling="no" style="border:none;overflow:hidden;width:90px;height:21px"></iframe><!--<![endif]--><!--[if IE]><iframe
frameborder="0" allowTransparency="true" class="addtoany_special_service twitter_tweet" src="http://platform.twitter.com/widgets/tweet_button.html?url=http%3A%2F%2Fwerxltd.com%2Fwp%2F2009%2F12%2F29%2Fsimple-java-implantation-of-json-rpc%2F&amp;counturl=http%3A%2F%2Fwerxltd.com%2Fwp%2F2009%2F12%2F29%2Fsimple-java-implantation-of-json-rpc%2F&amp;count=none&amp;text=Simple%20Java%20implementation%20of%20JSON-RPC" scrolling="no" style="border:none;overflow:hidden;width:55px;height:20px"></iframe><![endif]--><!--[if !IE]><!--><iframe
class="addtoany_special_service twitter_tweet" src="http://platform.twitter.com/widgets/tweet_button.html?url=http%3A%2F%2Fwerxltd.com%2Fwp%2F2009%2F12%2F29%2Fsimple-java-implantation-of-json-rpc%2F&amp;counturl=http%3A%2F%2Fwerxltd.com%2Fwp%2F2009%2F12%2F29%2Fsimple-java-implantation-of-json-rpc%2F&amp;count=none&amp;text=Simple%20Java%20implementation%20of%20JSON-RPC" scrolling="no" style="border:none;overflow:hidden;width:55px;height:20px"></iframe><!--<![endif]--><!--[if IE]><iframe
frameborder="0" allowTransparency="true" class="addtoany_special_service google_plusone" src="https://plusone.google.com/u/0/_/%2B1/fastbutton?url=http%3A%2F%2Fwerxltd.com%2Fwp%2F2009%2F12%2F29%2Fsimple-java-implantation-of-json-rpc%2F&amp;size=medium&amp;count=false" scrolling="no" style="border:none;overflow:hidden;width:32px;height:20px"></iframe><![endif]--><!--[if !IE]><!--><iframe
class="addtoany_special_service google_plusone" src="https://plusone.google.com/u/0/_/%2B1/fastbutton?url=http%3A%2F%2Fwerxltd.com%2Fwp%2F2009%2F12%2F29%2Fsimple-java-implantation-of-json-rpc%2F&amp;size=medium&amp;count=false" scrolling="no" style="border:none;overflow:hidden;width:32px;height:20px"></iframe><!--<![endif]--><a
class="a2a_button_linkedin" href="http://www.addtoany.com/add_to/linkedin?linkurl=http%3A%2F%2Fwerxltd.com%2Fwp%2F2009%2F12%2F29%2Fsimple-java-implantation-of-json-rpc%2F&amp;linkname=Simple%20Java%20implementation%20of%20JSON-RPC" title="LinkedIn" rel="nofollow" target="_blank"><img
src="http://werxltd.com/wp/wp-content/plugins/add-to-any/icons/linkedin.png?9d7bd4" width="16" height="16" alt="LinkedIn"/></a><a
class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fwerxltd.com%2Fwp%2F2009%2F12%2F29%2Fsimple-java-implantation-of-json-rpc%2F&amp;title=Simple%20Java%20implementation%20of%20JSON-RPC" id="wpa2a_2">Share/Save</a></p>]]></content:encoded> <wfw:commentRss>http://werxltd.com/wp/2009/12/29/simple-java-implantation-of-json-rpc/feed/</wfw:commentRss> <slash:comments>2</slash:comments> </item> </channel> </rss>
<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Minified using apc
Page Caching using apc
Database Caching 2/6 queries in 0.105 seconds using apc
Object Caching 367/367 objects using apc

Served from: werxltd.com @ 2012-02-08 13:38:43 -->
