Implemented an example for a controller, that fetches remote-data
[demos/testing] / src / main / java / de / juplo / integrationtest / RemoteContentController.java
1 package de.juplo.integrationtest;
2
3
4 import org.springframework.beans.factory.annotation.Autowired;
5 import org.springframework.beans.factory.annotation.Value;
6 import org.springframework.stereotype.Controller;
7 import org.springframework.ui.Model;
8 import org.springframework.web.bind.annotation.GetMapping;
9 import org.springframework.web.bind.annotation.RequestParam;
10 import org.springframework.web.reactive.function.client.WebClient;
11
12
13 /**
14  * Fetches data from remote-webserver and renders them as HTML.
15  * @author Kai Moritz
16  */
17 @Controller
18 public class RemoteContentController
19 {
20   WebClient webClient;
21
22
23   public RemoteContentController(@Value("${remote.host}")String remoteHost)
24   {
25     webClient = WebClient.create(remoteHost);
26   }
27
28
29   @GetMapping("/")
30   public String renderRemoteText(Model model, @RequestParam String path)
31   {
32     model.addAttribute(
33         "text",
34         webClient
35             .get()
36             .uri(path)
37             .retrieve()
38             .bodyToMono(String.class).block());
39
40     return "layout";
41   }
42 }