Class RxSON
public final class RxSON extends Object
- Since:
- 11
- Author:
- Mohamed Aly Bou Hanane
©2020
An Asynchronous Reactive JSON REST Client to stream any REST resource.
RxSON
makes it easy for the application to use JSON streamed chunks from the response as soon as they arrive, and rendering you code fastr. It treats the HTTP response as a series of small, useful chunks and map them to Java ObjectsAn
RxSON
can be used to send requests and retrieve their response as a reactive event-driven, and asynchronous by using observable sequences. It is useful to read lage jason payload without running out og Memory.An
HttpClient
is created through aRxSON.Builder().client(HttpClient) builder
. TheReactiveSubscriber
determines how to handle the data chunks received through stream and publish them to subscribers as java objects after mapping .Simple Example
String serviceURL = "https://think.cs.vt.edu/corgis/datasets/json/airlines/airlines.json"; HttpRequest req = HttpRequest.newBuilder(URI.create(serviceURL)).GET().build(); RxSON rxson = new RxSON.Builder().build(); String jsonPath = "$[*].Airport.Name"; Flowable<String> airportStream = rxson.create(String.class, req, jsonPath); airportStream .doOnNext(it -> System.out.println("Received new item: " + it)) //Just for test .toList() .blockingGet();
Detailed Example Create a class Model
public class Airline { @Reactive(path = "$[*].Airport") private Flowable<JsonNode> result; public Flowable<JsonNode> getResult() { return result; } public void setResult(final Flowable<JsonNode> result) { this.result = result; } }
RxSON rxrest = new RxSON.Builder().build(); HttpRequest req = HttpRequest.newBuilder(URI.create("my.service.com/airlines")).GET().build(); var airlinesStream = rxest.create(Airline.class, req); airlinesStream.getResult() .observeOn(Schedulers.io()) .subscribeOn(Schedulers.io()) .doOnNext(it -> System.out.println("Received a flow item: " + it.get("Name"))) .subscribe(); //Just for testing //.toList().blockingGet();
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
RxSON.Builder
Builder forRxSON
-
Method Summary
Modifier and Type Method Description <T> T
create(Class<T> clazz)
Sends the given request asynchronously, create instance of the class and create a reactive stream for class properties It will use default client with GET and http://localhost:8080/<T> T
create(Class<T> clazz, HttpRequest req)
Sends the given request asynchronously, create instance of the class and create a reactive stream for class properties<T> io.reactivex.Flowable<T>
create(Class<T> clazz, HttpRequest req, String jsonPath)
Sends the given request asynchronously, create instance of the class and create a reactive flowable of T<T extends CompletableStream<T>>
TcreateCompletable(Class<T> clazz, HttpRequest req)
Send async request, create instance of the class and create a reactive stream for class properties, as well as getting access to the responseOptional<HttpClient>
getClient()
-
Method Details
-
create
Sends the given request asynchronously, create instance of the class and create a reactive stream for class properties It will use default client with GET and http://localhost:8080/- Type Parameters:
T
- the response body type- Parameters:
clazz
- Target class- Returns:
- T an instance of the class
-
create
Sends the given request asynchronously, create instance of the class and create a reactive stream for class propertiesExample:
Flowable<Airport> airportStream = rxson.create(Airport.class, req, "$[*].Airport.Name");
- Type Parameters:
T
- the response body type- Parameters:
clazz
- Target classreq
- HttpRequest to send asynchronously- Returns:
- T an instance of the class
-
create
Sends the given request asynchronously, create instance of the class and create a reactive flowable of TExample:
Flowable<String> flow = rxson.create(String.class, req, "$[*].Airport.Name");
- Type Parameters:
T
- A flowable response of clazz- Parameters:
clazz
- Target class to create a flowable ofreq
- HttpRequest to send asynchronouslyjsonPath
- The json path to subscribe for- Returns:
- Flowable a flowable publisher of target type
-
createCompletable
Send async request, create instance of the class and create a reactive stream for class properties, as well as getting access to the response- Type Parameters:
T
- The generic type of the class- Parameters:
clazz
- Target class model that extendsCompletableStream
req
- HttpRequest to send asynchronously- Returns:
- CompletableStream of target type which allows access to the response as
CompletableStream.getAsyncResponse()
Example Example
getAsyncResponse() .whenComplete((r, t) -> System.out.println("--- Status code " + r.statusCode()))
-
getClient
- Returns:
- An
HttpClient
used to sendrequests
and retrieve theirresponses
.
-