From 760ff5d820afdeb2bf50b98c89519c384c56bf4b Mon Sep 17 00:00:00 2001 From: Kai Moritz Date: Sat, 11 Jan 2020 16:28:59 +0100 Subject: [PATCH] Implemented a service, that fetches data from a remote-host --- .../java/de/juplo/demo/DemoApplication.java | 18 ++++++-- .../de/juplo/demo/RemoteContentService.java | 43 +++++++++++++++++++ src/main/resources/application.properties | 2 +- 3 files changed, 58 insertions(+), 5 deletions(-) create mode 100644 src/main/java/de/juplo/demo/RemoteContentService.java diff --git a/src/main/java/de/juplo/demo/DemoApplication.java b/src/main/java/de/juplo/demo/DemoApplication.java index 76f6fe4..a065b88 100644 --- a/src/main/java/de/juplo/demo/DemoApplication.java +++ b/src/main/java/de/juplo/demo/DemoApplication.java @@ -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 index 0000000..c62225e --- /dev/null +++ b/src/main/java/de/juplo/demo/RemoteContentService.java @@ -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. + *

+ * The WebClient has to be configured to us a given + * remote-server by default. This service will only add a path. + * @param webClient a WebClient 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 getRemoteText(String path) + { + return + webClient + .get() + .uri(path) + .retrieve() + .bodyToMono(String.class); + } +} diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 8b13789..17c0d7b 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -1 +1 @@ - +remote.host=http://localhost/ -- 2.20.1