Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/emiperez/repeson

A JSON-RPC Client for Java
https://github.com/emiperez/repeson

asynchronous java-11 java-11-http-client json-rpc json-rpc-client json-rpc-transport json-rpc2 json-rpc2-client

Last synced: about 10 hours ago
JSON representation

A JSON-RPC Client for Java

Awesome Lists containing this project

README

        

# repeson
A JSON-RPC Client for Java.
It uses the HttpClient of JDK11, so requests can be sent either blocking (synchronous) or not blocking (asynchronous)
## How to use it
### 1. Adding the dependency to your project
```xml

com.emiperez.repeson
repeson
0.5.0

```
### 2. Configuring the Transport
1. Currently only HTTP/HTTPS Transport has been developed.
1. Create a new [HttpClient](https://openjdk.java.net/groups/net/httpclient/intro.html) and Configure it with any HTTP related properties (Authentication, Proxy, Cookie Handler, SSL and so on)
1. Build a new `HttpTransport` and inject the `HttpClient` to it
```java
Transport transport = HttpTransport.builder(httpClient).uri(uri).contentType(contentType).build();
```
### 3. Building the JsonRpcClient
```java
JsonRpcClient jsonRpcClient = JsonRpcClient.builder()
.transport(transport)
.version(JsonRpcVersion.v2_0)
.idGenerator(idGenerator)
.build();
```
### 4.- Sending the Request and getting the Response
Synchronous or blocking:
```java
JsonRpcResponse r = jsonRpcClient.sendRequestWithDefaults("getcustomer", paramsPojo);
r.ifHasResultOrElse(customer -> System.out.println(customer.getName(),
() -> System.out.println("No customer returned"));
```
or Asynchronous (not blocking)
```java
CompletableFuture cc = jsonRpcClient
.sendRequestWithDefaultsAsync("getcustomer", paramsPojo)
.thenApply(JsonRpcResponse::getResult);
```
If the returned Type uses Generics, for example; `ArrayList`, to prevent the [Type Erasure](https://docs.oracle.com/javase/tutorial/java/generics/erasure.html), a class file, that extends JsonRpcResponse must be created,
```java
public class CustomerListResponse extends JsonRpcResponse> {}
```
and passed as an argument to send methods:
```java
CustomerListResponse r = jsonRpcClient.sendRequestWithDefaults("listcustomers", paramsPojo,
CustomerListResponse.class);
if (r.hasResult()) {
ArrayList cs = r.getResult();
//Do whatever with cs
}
```