# jaspercloud-react-common **Repository Path**: jaspercloud/jaspercloud-react-common ## Basic Information - **Project Name**: jaspercloud-react-common - **Description**: react-common - **Primary Language**: Java - **License**: Apache-2.0 - **Default Branch**: main - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2021-10-26 - **Last Updated**: 2021-10-26 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # jaspercloud-react-common ## react-http-client ``` java ReactHttpClient reactHttpClient = new ReactHttpClient(1); reactHttpClient.execute(new Request.Builder() .url("http://www.baidu.com") .build()) .timeout(3000) .then(new ReactAsyncCall() { private long start; @Override public void onSubscribe() { start = System.currentTimeMillis(); } @Override public void onFinally() { //记录请求时间 System.out.println("runTime: " + (System.currentTimeMillis() - start)); } @Override public void process(boolean hasError, Throwable throwable, Response result, ReactSink sink) throws Throwable { //Response处理 if (hasError) { throw throwable; } try (Response response = result) { sink.success(response.body().string()); } } }).then(new ReactAsyncCall() { @Override public void process(boolean hasError, Throwable throwable, String result, ReactSink sink) throws Throwable { //链式处理 System.out.println(result); } }).subscribe(); ``` ## Springboot Controller中使用 ``` java @GetMapping("/test") public DeferredResult> test() throws Exception { return Async.run(new Async.AsyncCallable>() { @Override protected Mono> call() { Request request = new Request.Builder() .url("http://www.baidu.com") .build(); //请求 Mono mono = reactHttpClient.execute(request).toMono(); AsyncMono> result = new AsyncMono<>(mono).then(new ReactSyncCall>() { @Override public ResponseEntity process(boolean hasError, Throwable throwable, Response in) throws Throwable { //Response处理 if (hasError) { throw throwable; } int code = in.code(); if (200 != code) { throw new RuntimeException(); } return ResponseEntity.ok(in.body().string()); } }); return result.toMono(); } @Override protected ResponseEntity onThrowable(Throwable e) { return ResponseEntity.status(500).body(e.getMessage()); } @Override protected void onTimeout() { super.onTimeout(); } }); } ```