Implemented an example for a controller, that fetches remote-data rm
authorKai Moritz <kai@juplo.de>
Fri, 10 Jan 2020 18:44:03 +0000 (19:44 +0100)
committerKai Moritz <kai@juplo.de>
Fri, 10 Jan 2020 18:44:03 +0000 (19:44 +0100)
src/main/java/de/juplo/integrationtest/RemoteContentController.java [new file with mode: 0644]
src/main/resources/application.properties
src/main/resources/templates/layout.html [new file with mode: 0644]

diff --git a/src/main/java/de/juplo/integrationtest/RemoteContentController.java b/src/main/java/de/juplo/integrationtest/RemoteContentController.java
new file mode 100644 (file)
index 0000000..59f342e
--- /dev/null
@@ -0,0 +1,42 @@
+package de.juplo.integrationtest;
+
+
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.stereotype.Controller;
+import org.springframework.ui.Model;
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.RequestParam;
+import org.springframework.web.reactive.function.client.WebClient;
+
+
+/**
+ * Fetches data from remote-webserver and renders them as HTML.
+ * @author Kai Moritz
+ */
+@Controller
+public class RemoteContentController
+{
+  WebClient webClient;
+
+
+  public RemoteContentController(@Value("${remote.host}")String remoteHost)
+  {
+    webClient = WebClient.create(remoteHost);
+  }
+
+
+  @GetMapping("/")
+  public String renderRemoteText(Model model, @RequestParam String path)
+  {
+    model.addAttribute(
+        "text",
+        webClient
+            .get()
+            .uri(path)
+            .retrieve()
+            .bodyToMono(String.class).block());
+
+    return "layout";
+  }
+}
diff --git a/src/main/resources/templates/layout.html b/src/main/resources/templates/layout.html
new file mode 100644 (file)
index 0000000..1087568
--- /dev/null
@@ -0,0 +1,11 @@
+<!DOCTYPE HTML>
+<html xmlns:th="http://www.thymeleaf.org">
+<head>
+  <title>Shows Remote-Content</title>
+  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
+</head>
+<body>
+  <h1>Remote-Content:</h1>
+  <p th:text="${text}">TEXT</p>
+</body>
+</html>