Implemented a service, that fetches data from a remote-host
authorKai Moritz <kai@juplo.de>
Sat, 11 Jan 2020 15:28:59 +0000 (16:28 +0100)
committerKai Moritz <kai@juplo.de>
Mon, 13 Jan 2020 18:40:01 +0000 (19:40 +0100)
src/main/java/de/juplo/demo/DemoApplication.java
src/main/java/de/juplo/demo/RemoteContentService.java [new file with mode: 0644]
src/main/resources/application.properties

index 76f6fe4..a065b88 100644 (file)
@@ -1,13 +1,23 @@
 package de.juplo.demo;
 
+import org.springframework.beans.factory.annotation.Value;
 import org.springframework.boot.SpringApplication;
 import org.springframework.boot.autoconfigure.SpringBootApplication;
+import org.springframework.context.annotation.Bean;
+import org.springframework.web.reactive.function.client.WebClient;
 
 @SpringBootApplication
-public class DemoApplication {
+public class DemoApplication
+{
+  @Bean
+  public RemoteContentService service(@Value("${remote.host}")String remoteHost)
+  {
+    return new RemoteContentService(WebClient.create(remoteHost));
+  }
 
-       public static void main(String[] args) {
-               SpringApplication.run(DemoApplication.class, args);
-       }
 
+  public static void main(String[] args)
+  {
+    SpringApplication.run(DemoApplication.class, args);
+  }
 }
diff --git a/src/main/java/de/juplo/demo/RemoteContentService.java b/src/main/java/de/juplo/demo/RemoteContentService.java
new file mode 100644 (file)
index 0000000..c62225e
--- /dev/null
@@ -0,0 +1,43 @@
+package de.juplo.demo;
+
+import org.springframework.web.reactive.function.client.WebClient;
+import reactor.core.publisher.Mono;
+
+
+/**
+ * Fetches data from remote-webserver.
+ * @author Kai Moritz
+ */
+public class RemoteContentService
+{
+  WebClient webClient;
+
+
+  /**
+   * The {@link WebClient}, that is used to fetch the data.
+   * <p>
+   * The <code>WebClient</code> has to be configured to us a given
+   * remote-server by default. This service will only add a path.
+   * @param webClient a <code>WebClient</code> instance with configured host
+   */
+  public RemoteContentService(WebClient webClient)
+  {
+    this.webClient = webClient;
+  }
+
+
+  /**
+   * Fetches the given path from the configured remote-server.
+   * @param path the path to fetch from the configured remote-server
+   * @return a {@link Mono}, that represents the fetched data
+   */
+  public Mono<String> getRemoteText(String path)
+  {
+    return
+        webClient
+            .get()
+            .uri(path)
+            .retrieve()
+            .bodyToMono(String.class);
+  }
+}