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));
}
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<String> renderRemoteText(@RequestParam String path)
{
- model.addAttribute(
- "text",
- webClient
- .get()
- .uri(path)
- .retrieve()
- .bodyToMono(String.class).block());
-
- return "layout";
+ return service.getRemoteText(path);
}
}
--- /dev/null
+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);
+ }
+}
+++ /dev/null
-<!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>