From: Kai Moritz Date: Sat, 11 Jan 2020 08:52:21 +0000 (+0100) Subject: WIP:service X-Git-Tag: tmp~4 X-Git-Url: https://juplo.de/gitweb/?p=demos%2Ftesting;a=commitdiff_plain;h=ff867961efa926673ae70ae06fd1e0d4bc861f53 WIP:service --- diff --git a/src/main/java/de/juplo/demo/DemoApplication.java b/src/main/java/de/juplo/demo/DemoApplication.java index 53303c5..88b890c 100644 --- a/src/main/java/de/juplo/demo/DemoApplication.java +++ b/src/main/java/de/juplo/demo/DemoApplication.java @@ -4,15 +4,15 @@ 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 { @Bean - public RemoteContentController remoteContentController( - @Value("${remote.host}")String remoteHost) + public RemoteContentService service(@Value("${remote.host}")String remoteHost) { - return new RemoteContentController(remoteHost); + return new RemoteContentService(WebClient.create(remoteHost)); } diff --git a/src/main/java/de/juplo/demo/RemoteContentController.java b/src/main/java/de/juplo/demo/RemoteContentController.java index fa0e49a..00b0b96 100644 --- a/src/main/java/de/juplo/demo/RemoteContentController.java +++ b/src/main/java/de/juplo/demo/RemoteContentController.java @@ -1,41 +1,26 @@ package de.juplo.demo; - -<<<<<<< HEAD:src/main/java/de/juplo/demo/RemoteContentController.java -======= ->>>>>>> f9152f7... WIP: Implemented...:src/main/java/de/juplo/integrationtest/RemoteContentController.java -import org.springframework.ui.Model; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestParam; -import org.springframework.web.reactive.function.client.WebClient; +import org.springframework.web.bind.annotation.RestController; +import reactor.core.publisher.Mono; /** * Fetches data from remote-webserver and renders them as HTML. * @author Kai Moritz */ +@RestController public class RemoteContentController { - WebClient webClient; - - - public RemoteContentController(WebClient webClient) - { - this.webClient = webClient; - } + @Autowired + RemoteContentService service; @GetMapping("/") - public String renderRemoteText(Model model, @RequestParam String path) + public Mono renderRemoteText(@RequestParam String path) { - model.addAttribute( - "text", - webClient - .get() - .uri(path) - .retrieve() - .bodyToMono(String.class).block()); - - return "layout"; + return service.getRemoteText(path); } } 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/templates/layout.html b/src/main/resources/templates/layout.html deleted file mode 100644 index 1087568..0000000 --- a/src/main/resources/templates/layout.html +++ /dev/null @@ -1,11 +0,0 @@ - - - - Shows Remote-Content - - - -

Remote-Content:

-

TEXT

- -