@Test public void testFileDownload() throws InterruptedException, ExecutionException { ClientMain.saveResponseToFile(webClient) .thenAccept(it -> assertThat(Files.exists(testFile), is(true))) .thenAccept(it -> { try { assertThat(Files.readString(testFile), is("{\"message\":\"Hello World!\"}")); } catch (IOException e) { fail(e); } }) .toCompletableFuture() .get(); }
static CompletionStage<Void> saveResponseToFile(WebClient webClient) { Path file = Paths.get("test.txt"); try { Files.deleteIfExists(file); } catch (IOException e) { e.printStackTrace(); } FileSubscriber fileSubscriber = FileSubscriber.create(file); System.out.println("Downloading server response to the file: " + file); return webClient.get() .request() .thenApply(WebClientResponse::content) .thenCompose(fileSubscriber::subscribeTo) .thenAccept(path -> System.out.println("Download complete!")); }
ClientMain { static CompletionStage<Void> saveResponseToFile(WebClient webClient) { Path file = Paths.get("test.txt"); try { Files.deleteIfExists(file); } catch (IOException e) { e.printStackTrace(); } FileSubscriber fileSubscriber = FileSubscriber.create(file); System.out.println("Downloading server response to the file: " + file); return webClient.get() .request() .thenApply(WebClientResponse::content) .thenCompose(fileSubscriber::subscribeTo) .thenAccept(path -> System.out.println("Download complete!")); } }
ClientMain { static CompletionStage<Void> saveResponseToFile(WebClient webClient) { Path file = Paths.get("test.txt"); try { Files.deleteIfExists(file); } catch (IOException e) { e.printStackTrace(); } FileSubscriber fileSubscriber = FileSubscriber.create(file); System.out.println("Downloading server response to the file: " + file); return webClient.get() .request() .thenApply(WebClientResponse::content) .thenCompose(fileSubscriber::subscribeTo) .thenAccept(path -> System.out.println("Download complete!")); } private  ClientMain(); }
ClientMain { static CompletionStage<Void> saveResponseToFile(WebClient webClient) { Path file = Paths.get("test.txt"); try { Files.deleteIfExists(file); } catch (IOException e) { e.printStackTrace(); } FileSubscriber fileSubscriber = FileSubscriber.create(file); System.out.println("Downloading server response to the file: " + file); return webClient.get() .request() .thenApply(WebClientResponse::content) .thenCompose(fileSubscriber::subscribeTo) .thenAccept(path -> System.out.println("Download complete!")); } private  ClientMain(); static void main(String[] args); }
ClientMain { static CompletionStage<Void> saveResponseToFile(WebClient webClient) { Path file = Paths.get("test.txt"); try { Files.deleteIfExists(file); } catch (IOException e) { e.printStackTrace(); } FileSubscriber fileSubscriber = FileSubscriber.create(file); System.out.println("Downloading server response to the file: " + file); return webClient.get() .request() .thenApply(WebClientResponse::content) .thenCompose(fileSubscriber::subscribeTo) .thenAccept(path -> System.out.println("Download complete!")); } private  ClientMain(); static void main(String[] args);  }