Skip to content

JSON RPC Service

StephenCote edited this page Oct 1, 2013 · 3 revisions

Overview

The JSON RPC Service generates JSON client stubs for registered services, dynamically or statically defines the service methods, and dynamically or statically injects JavaScript entities of any schema exposed by the service.

Server Configuration

Refer to the HemiWebExample or AccountManagerExample projects for an example using JBoss 7.x and Jackson. Alternately, use Tuscany with JSON-RPC end points configured.

Server End Points

  • Simple Method Detection (/smd)
  • Entity Schema (/entity) : Lightweight schema definition by example of JSON objects to make available

Client Service Registration

Use the addService method to register a JSON RPC service, including the smd end point.

var vGetSmd = true;
var vGetEntity = true;
var bCache = true;
Hemi.json.rpc.service.addService("DemoService", "/DemoApp/DemoService/smd", vGetSmd, vGetEntity, bCache);

Alternately, instead of dynamically polling the service each time, the JSON schema may be specified instead of the boolean value.

var vGetSmd = {...};
var vGetEntity = {...};
var bCache = true;
Hemi.json.rpc.service.addService(
    "DemoService",
    "/DemoApp/DemoService/smd",
    vGetSmd,
    vGetEntity,
    bCache
);

Client Use

Once the service is registered, it may be retrieved with the getService method. Public methods defined for the service are built into the service object.

var oService = Hemi.json.rpc.service.getService("DemoService");
oService.myMethod();

Entity Use

If an entity schema is provided, then those entities will be added and accessible as first class objects.

var oObject = new com.mypackage.mystuff.object();
oService.add(oObject);

Example Using HemiWebExample/ Java Application

Installation

Package and deploy the HemiFramework/ and HemiWebExample/ Java Applications. A Jboss-AS 7 plugin is included. A WebSphere 8.5 Liberty plugin may be included, but the following example will use Jboss for testing.

Package and Deploy the Framework

Note: Make sure the ./src/main/webapp/Hemi link is correct, or copy the contents of the ./Hemi project into the ./src/main/webapp/Hemi directory

cd src/HemiFramework
mvn clean package jboss-as:deploy

Package and Deploy the Sample Application

cd src/HemiWebExample
mvn clean package jboss-as:deploy

Navigate to Example Page

Open a browser and navigate to http://localhost:8080/HemiWebExample. The Hemi Logo will be displayed, along with a toolbar of icons. If a script error occurs, verify the HemiFramework application was deployed correctly. Click the top-left icon to open the Hemi Framework Profiler, and click the Active Source tab.

Accessing the Service

Using the Active Source window (or your prefered JavaScript console), verify the service exists by typing the following in the text box and pressing enter:

Hemi.json.rpc.service.getService("ExampleService");

Click the Reflect button and verify that the new and update methods are present.

Type the following in and verify the entities were created on the page:

new org.cote.example.objects.example()

Click the Reflect button and verify that the properties defined in the XSD schema are displayed in the window as JSON properties. Or, more simply, verify there is no error message.

Next, try creating a new object from the server:

var oNew = Hemi.json.rpc.service.getService("ExampleService").new();

And, type oNew into the text field and click Reflect to view the properties.

Next, try updating the object on the server:

Hemi.json.rpc.service.getService("ExampleService").update(oNew);

Instead of pressing Enter, just click Reflect and note that the modified date should change each time.

Clone this wiki locally