json-client-commons

a library for creating JSON RPC services in Java

License

License

json-client-commons is licensed unerl LGPL
Categories

Categories

CLI User Interface JSON Data
GroupId

GroupId

ca.uhn.ws
ArtifactId

ArtifactId

json-client-commons
Last Version

Last Version

2.1
Release Date

Release Date

Type

Type

jar
Description

Description

json-client-commons
a library for creating JSON RPC services in Java
Project URL

Project URL

https://github.com/uhnuser/json-client-commons
Project Organization

Project Organization

University Health Network
Source Code Management

Source Code Management

https://github.com/uhnuser/json-client-commons

Download json-client-commons

How to add to project

<!-- https://jarcasting.com/artifacts/ca.uhn.ws/json-client-commons/ -->
<dependency>
    <groupId>ca.uhn.ws</groupId>
    <artifactId>json-client-commons</artifactId>
    <version>2.1</version>
</dependency>
// https://jarcasting.com/artifacts/ca.uhn.ws/json-client-commons/
implementation 'ca.uhn.ws:json-client-commons:2.1'
// https://jarcasting.com/artifacts/ca.uhn.ws/json-client-commons/
implementation ("ca.uhn.ws:json-client-commons:2.1")
'ca.uhn.ws:json-client-commons:jar:2.1'
<dependency org="ca.uhn.ws" name="json-client-commons" rev="2.1">
  <artifact name="json-client-commons" type="jar" />
</dependency>
@Grapes(
@Grab(group='ca.uhn.ws', module='json-client-commons', version='2.1')
)
libraryDependencies += "ca.uhn.ws" % "json-client-commons" % "2.1"
[ca.uhn.ws/json-client-commons "2.1"]

Dependencies

compile (5)

Group / Artifact Type Version
ca.uhn.ws : json-rpc-model jar 1.0
commons-httpclient : commons-httpclient jar 3.1
commons-codec : commons-codec jar 1.4
commons-io : commons-io jar 1.4
com.google.code.gson : gson jar 2.1

provided (1)

Group / Artifact Type Version
log4j : log4j jar 1.2.8

Project Modules

There are no modules declared in this project.

json-client-commons

a library for creating JSON RPC clients in Java

Installation

Artifact is provided as a Maven dependency

<dependency>
	<groupId>ca.uhn.ws</groupId>
	<artifactId>json-client-commons</artifactId>
	<version>2.1</version>
</dependency>

Usage

To make a JSON RPC client you will need to do the following things:

Extend ca.uhn.model.json.BaseRequestParams with a class containing the parameters for your request:

import ca.uhn.model.json.BaseRequestParams;

public class RequestParams extends BaseRequestParams {
  public String id;  
}

Create a class into which the result will be deserialized

import java.util.Date;

public class Person {  
  public String name;
  public Integer height;
  public Date age;
}

The client uses Google Gson library to serialize and deserialize classes.

Extend ca.uhn.json.client.JsonClient with your class

import java.util.Date;
import ca.uhn.json.client.JsonClient;

public class MyClient extends JsonClient {
    
    //specify the type into which the response will be deserialized
    private static final Type PERSON = new TypeToken<Person>(){}.getType();
    //specify the name of the operation which will be called on the service
    private static final String OPERATION = "getPerson";
    
    public MyClient(String url) {
      super(url);
    }
    
    public Person getPerson(String id) throws Exception {
      RequestParams params = new RequestParams();
      params.id = id;      
      return callService(OPERATION, params, PERSON);
    }

    public static void main(String[] args) throws Exception {
  	  System.out.println(new MyClient("http://localhost:8080/test-service/json-service").getPerson("123"));
	}
}

The client will generate the following JSON when callService executes:

POST /test-service/json-service HTTP/1.1
User-Agent: Jakarta Commons-HttpClient/3.1
Host: localhost:8080
Content-Length: 82
Content-Type: application/json; charset=UTF-8

{
  "jsonrpc": "2.0",
  "method": "getPerson",
  "params": {
    "id": "123"
  }
}

The service should reply with a response which looks like the following:

HTTP/1.1 200 OK
X-Powered-By: Servlet/2.5
Server: Sun GlassFish Enterprise Server v2.1.1
Content-Type: application/json;charset=ISO-8859-1
Transfer-Encoding: chunked
Date: Wed, 10 Oct 2012 20:59:36 GMT

{
  "jsonrpc": "2.0",
  "result": {
    "name": "John",
    "height": 180,
    "age": "Oct 10, 2012 04:59:36 PM"
  }
}

note that dates are serialized using the following format: MMM dd, yyyy hh:mm:ss a

Versions

Version
2.1